Javascript 为什么赢了';我的json对象的'email'属性是否显示在我的jade模板中?

Javascript 为什么赢了';我的json对象的'email'属性是否显示在我的jade模板中?,javascript,node.js,express,mongoose,pug,Javascript,Node.js,Express,Mongoose,Pug,有最奇怪的问题。我无法显示对象的电子邮件属性。我用的是猫鼬/Express/Jade。我正在做一个: User.find({ _id : { $in : req.project.subscribers } } , function (err, subs){... 它工作正常,在subs变量中返回我的订阅者列表,如下所示: { _id: 4f34832b9398bec847000002, email: 'steve@example.com', first_name: 'Steve',

有最奇怪的问题。我无法显示对象的
电子邮件
属性。我用的是猫鼬/Express/Jade。我正在做一个:

User.find({ _id : { $in : req.project.subscribers } } , function (err, subs){...
它工作正常,在subs变量中返回我的订阅者列表,如下所示:

{ _id: 4f34832b9398bec847000002,
  email: 'steve@example.com',
  first_name: 'Steve',
  last_name: 'McQueen',
  status: 'active',
  username: 'steve',
  updated_at: Fri, 10 Feb 2012 02:38:35 GMT,
  created_at: Fri, 10 Feb 2012 02:38:35 GMT,
  role: 'subscriber' },

  { _id: 4f3484a9eceb82b548000002,
  email: 'paul@example.com',
  first_name: 'Paul',
  last_name: 'Stanley',
  status: 'active',
  username: 'paul',
  updated_at: Mon, 02 Apr 2012 15:09:56 GMT,
  created_at: Mon, 02 Apr 2012 15:09:56 GMT,
  role: 'subscriber' }
然后,我将其传递到名为subscribers的var中的jade视图。 至此,当我在jade模板中迭代
subscribers
时,当我想显示每个订阅者的
email
属性时,问题就出现了。这只是由于某种原因,
电子邮件的问题!对于
电子邮件
,它不显示任何内容,只是空白

ul
  each sub in subscribers
    li= sub.email
如果我用任何其他属性代替
电子邮件
,效果很好。因此,以下显示非常好:

ul
  each sub in subscribers
    li= sub.username
请让我知道,如果我可以提供任何更多的细节

编辑德克兰:

执行以下操作:

ul
    each val, key in subscribers
     li #{key}: #{val}
收益率:

    0: { _id: 4f34832b9398bec847000002, email: 'foo', first_name: 'Steve', last_name: 
'McQueen', status: 'active', username: 'steve', updated_at: Fri, 10 Feb 2012 02:38:35 GMT, 
created_at: Fri, 10 Feb 2012 02:38:35 GMT, role: 'subscriber' }

1: { _id: 4f3484a9eceb82b548000002, email: 'foo', first_name: 'Paul', last_name: 'Stanley', 
status: 'active', username: 'paul', updated_at: Mon, 02 Apr 2012 16:05:51 GMT, created_at: 
Mon, 02 Apr 2012 16:05:51 GMT, role: 'subscriber' }

在我看来,翡翠是对的,可能是您的请求处理程序中的某些内容,以及您如何将其添加到要呈现的视图中。以下是一个简单的例子:

服务器端请求处理程序

app.get('/', function(req, res) {
    var users = [{
        _id: '4f34832b9398bec847000002',
        email: 'steve@example.com',
        first_name: 'Steve',
        last_name: 'McQueen',
        status: 'active',
        username: 'steve',
        updated_at: "Fri, 10 Feb 2012 02:38:35 GMT",
        created_at: "Fri, 10 Feb 2012 02:38:35 GMT",
        role: 'subscriber'
    }, {
        _id: '4f3484a9eceb82b548000002',
        email: 'paul@example.com',
        first_name: 'Paul',
        last_name: 'Stanley',
        status: 'active',
        username: 'paul',
        updated_at: "Mon, 02 Apr 2012 15:09:56 GMT",
        created_at: "Mon, 02 Apr 2012 15:09:56 GMT",
        role: 'subscriber'
    }];
    res.render('index.jade', {subscribers : users });
});
目录索引。翡翠

html
    body
        ul
            each sub in subscribers
                li= sub.email
输出

<html>
    <body>
        <ul>
            <li>steve@example.com</li>
            <li>paul@example.com</li>
        </ul>
    </body>
</html>

  • steve@example.com
  • paul@example.com

我觉得翡翠看起来不错,可能是您的请求处理程序中的某些内容,以及您如何将其添加到要呈现的视图中。以下是一个简单的例子:

服务器端请求处理程序

app.get('/', function(req, res) {
    var users = [{
        _id: '4f34832b9398bec847000002',
        email: 'steve@example.com',
        first_name: 'Steve',
        last_name: 'McQueen',
        status: 'active',
        username: 'steve',
        updated_at: "Fri, 10 Feb 2012 02:38:35 GMT",
        created_at: "Fri, 10 Feb 2012 02:38:35 GMT",
        role: 'subscriber'
    }, {
        _id: '4f3484a9eceb82b548000002',
        email: 'paul@example.com',
        first_name: 'Paul',
        last_name: 'Stanley',
        status: 'active',
        username: 'paul',
        updated_at: "Mon, 02 Apr 2012 15:09:56 GMT",
        created_at: "Mon, 02 Apr 2012 15:09:56 GMT",
        role: 'subscriber'
    }];
    res.render('index.jade', {subscribers : users });
});
目录索引。翡翠

html
    body
        ul
            each sub in subscribers
                li= sub.email
输出

<html>
    <body>
        <ul>
            <li>steve@example.com</li>
            <li>paul@example.com</li>
        </ul>
    </body>
</html>

  • steve@example.com
  • paul@example.com

发布作为答案,以便更好地设置代码格式

我刚到家,准备了一个测试应用程序,看看是否可以复制你的错误。到目前为止我还没能做到。这是我的档案

注意:我没有使用数据库进行测试,因此问题可能是如何将数据从数据库中获取到模板中:

翡翠索引

ul
  each sub in subscribers
    li= sub.email
routes/index.js

exports.index = function(req, res){
  res.render('index', { subscribers: [{ "_id": "4f34832b9398bec847000002",  "email": "steve@example.com",  "first_name": "Steve",  "last_name": "McQueen",  "status": "active",  "username": "steve",  "updated_at": "Fri, 10 Feb 2012 02:38:35 GMT",  "created_at": "Fri, 10 Feb 2012 02:38:35 GMT",  "role": "subscriber" },  { "_id": "4f3484a9eceb82b548000002",  "email": "paul@example.com",  "first_name": "Paul",  "last_name": "Stanley",  "status": "active",  "username": "paul",  "updated_at": "Mon, 02 Apr 2012 15:09:56 GMT",  "created_at": "Mon, 02 Apr 2012 15:09:56 GMT",  "role": "subscriber" }] })
};
app.js

var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(8000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

你能发布你的routes/app.get函数吗?所有的数据库处理代码也会被发布吗?

作为答案发布,以便更好地设置代码格式

我刚到家,准备了一个测试应用程序,看看是否可以复制你的错误。到目前为止我还没能做到。这是我的档案

注意:我没有使用数据库进行测试,因此问题可能是如何将数据从数据库中获取到模板中:

翡翠索引

ul
  each sub in subscribers
    li= sub.email
routes/index.js

exports.index = function(req, res){
  res.render('index', { subscribers: [{ "_id": "4f34832b9398bec847000002",  "email": "steve@example.com",  "first_name": "Steve",  "last_name": "McQueen",  "status": "active",  "username": "steve",  "updated_at": "Fri, 10 Feb 2012 02:38:35 GMT",  "created_at": "Fri, 10 Feb 2012 02:38:35 GMT",  "role": "subscriber" },  { "_id": "4f3484a9eceb82b548000002",  "email": "paul@example.com",  "first_name": "Paul",  "last_name": "Stanley",  "status": "active",  "username": "paul",  "updated_at": "Mon, 02 Apr 2012 15:09:56 GMT",  "created_at": "Mon, 02 Apr 2012 15:09:56 GMT",  "role": "subscriber" }] })
};
app.js

var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(8000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

你能发布你的routes/app.get函数吗?所有的数据库处理代码都会显示出来吗?

我在回答我自己的问题,因为它最终变得愚蠢而明显(通常是这样,不是吗?)

当使用mongoose时,如果没有显示属性,请确保在mongoose模式中定义了该属性。不知怎的,我在创建模式时忽略了email字段,只是没有定义它。即使它在数据库中的实际记录中,mongoose也不在乎,只会显示您为对象/模式定义的内容


感谢大家的努力,如果没有别的,你帮助我得到了我的答案

我回答我自己的问题是因为它最终变成了愚蠢和明显的事情(通常是这样,不是吗?)

当使用mongoose时,如果没有显示属性,请确保在mongoose模式中定义了该属性。不知怎的,我在创建模式时忽略了email字段,只是没有定义它。即使它在数据库中的实际记录中,mongoose也不在乎,只会显示您为对象/模式定义的内容


感谢大家的努力,如果没有别的,你帮助我得到了我的答案

如果只是为了测试,您在控制器中添加了循环,并尝试对其中的每个子项进行console.log,它显示了什么?你的财产在里面吗?是的,它在里面,那是我复制和粘贴全部物品的地方。我想知道jade是否对电子邮件中的特殊字符有问题。您是否尝试过删除@和。“并测试它们是否是问题的根源?”DeclanCook认为不错,但没有成功。我将所有电子邮件帐户值设置为foo,问题仍然存在。非常奇怪,您是否厌倦了使用符号:each val,key in obj li#{key}:#{val}如果只是为了测试,您在控制器中添加了循环,并尝试console.log其中的每个子项,它会显示什么?你的财产在里面吗?是的,它在里面,那是我复制和粘贴全部物品的地方。我想知道jade是否对电子邮件中的特殊字符有问题。您是否尝试过删除@和。“并测试它们是否是问题的根源?”DeclanCook认为不错,但没有成功。我将所有电子邮件帐户值设置为foo,问题仍然存在。非常奇怪,您是否厌倦了使用符号:each val,key in obj li{key}:{val}谢谢您的测试。我认为这可能与猫鼬如何返回该对象有关。我不认为它会成为一个真正的数组。当我将Mongoose的结果(subs)记录到console.log中时,我只看到两个逗号分隔的对象,就像我在原始文章中显示的那样。不确定那是对的。@k00k,是对的。当您对数组进行console.log时,它会执行一个toString,返回以逗号分隔的数组值列表。感谢您的测试。我认为这可能与猫鼬如何返回该对象有关。我不认为它会成为一个真正的数组。当我将Mongoose的结果(subs)记录到console.log中时,我只看到两个逗号分隔的对象,就像我在原始文章中显示的那样。不确定那是对的。@k00k,是对的。当你对一个数组进行console.log时,它会执行一个toString,返回一个逗号分隔的数组值列表。我刚刚用console记录了数据库的结果,正如预期的那样,所有属性看起来都很好,包括email。然而,我当时是伊泰拉