题
我有一个错误已经困扰我了几天。我对Node和Jade模板系统非常陌生,因此请忍受:我希望以以下方式添加样式表:
app.js(express):
app.get('/', loadUser, function(req, res) {
var User = req.user;
// console.log(User.groups[2]);
// var groups = User.groups.split(',');
// OK DUh. This only gets called when the client has the script Socket.IO
// and client runs socket.connect()
getMessages(User, function(messages) {
var locals = {
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
'index.js'
],
stylesheets: [
'index.css'
],
user : User,
messages: messages
};
console.log('ok');
res.render('app.jade', {locals : locals});
});
});
在layout.jade(使用app.jade执行)中,我有:
!!! 5
html
head
title UI
link(rel='stylesheet', href = 'stylesheets/reset.css')
link(rel='stylesheet', href = 'stylesheets/layout.css')
- var stylesheets = stylesheets || [];
#{stylesheets}
- each stylesheet in stylesheets
- if(stylesheet.indexOf('http') >= 0)
link(rel='stylesheet', href = stylesheet)
- else
link(rel='stylesheet', href = "stylesheets/"+stylesheet )
加上更多...我一直遇到相同的错误:
9. ' - if(stylesheet.indexOf(\'http') >= 0)'
Object function () {
var o = {}, i, l = this.length, r = [];
for(i=0; i
for(i in o) r.push(o[i]);
return r;
} has no method 'indexOf'
现在..陷阱是,这个确切的模板在另一个通过完全相同的变量中传递的应用程序中起作用:我真的很感谢你们对这个棘手问题的建议!
谢谢!马特·穆勒(Matt Mueller)
解决方案
所以这是你的问题...
在这一行中:
res.render('app.jade', {locals : locals});
您正在当地人==>当地人,这是一个哈希(好吧,我是佩尔人,我认为JS称他们为“关联数组”)
因此,现在在您的玉模板内,我们有了线:
- var stylesheets = stylesheets || [];
在Jade内部,您已经定义了变量“当地人”,但是其他所有内容都隐藏在此下面,因此未定义变量“样式表”(local.stylesheets是定义的)。因此,这条代码线将变量“样式表”设置为“ []”
因此,这是我必须推测的地方。 “索引”是数组对象的一种方法。也许在Jade内部构建的数组没有此方法,而在Node中构造的数组确实具有此方法。这将解释为什么您会遇到错误尝试调用“ stylesheets.indexof(...)”的错误。
不隶属于 StackOverflow