简体   繁体   English

为什么我的json对象的`email`属性不会显示在我的Jade模板中?

[英]Why won't the `email` property of my json object display in my jade template?

Having the strangest problem. 有最奇怪的问题。 I can't display the email property of an object. 我无法显示对象的email属性。 I'm using Mongoose/Express/Jade. 我正在使用猫鼬/ Express /玉。 I'm doing a: 我正在做:

User.find({ _id : { $in : req.project.subscribers } } , function (err, subs){...

Which works just fine and returns a list of my subscribers in the subs var that looks like so: 这很好用,并在subs var中返回我的订阅者列表,如下所示:

{ _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' }

I then pass that to my jade view in a var called subscribers. 然后,我将其传递给名为“订户”的var中的玉视图。 All good to this point, the problem arises when I want to display the email property of each subscriber when I iterate over subscribers in my jade template. 所有好这一点,当我想要显示的问题就出现了email ,当我遍历每个用户的属性subscribers在我玉模板。 It's only a problem with email for some reason! 出于某种原因,这只是email的问题! It doesn't show anything for email , just blank. 它没有显示任何email ,只是空白。

ul
  each sub in subscribers
    li= sub.email

It works fine if I substitute ANY of the other properties in place of email . 如果我用其他任何属性代替email ,效果都很好。 So the following displays perfectly fine: 因此,以下显示完全正常:

ul
  each sub in subscribers
    li= sub.username

Please let me know if I can supply any more detail. 如果可以提供更多详细信息,请告诉我。

EDIT for Declan: 编辑Declan:

Doing the following: 执行以下操作:

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

Yields: 产量:

    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' }

I'm answering my own question because it ended up being something stupid and obvious (usually is, isn't it?). 我之所以回答自己的问题,是因为它最终变得愚蠢而明显(通常是,不是吗?)。

When using mongoose and a property doesn't show up, be sure that you have that property defined in your mongoose schema. 当使用猫鼬并且没有显示属性时,请确保在猫鼬模式中定义了该属性。 Somehow, I overlooked the email field when I created my schema and it just wasn't defined. 不知何故,我在创建架构时就忽略了电子邮件字段,只是未定义它。 Even though it was in the actual record in the DB, mongoose doesn't care and will only show you what you've defined for the object/schema. 即使它在数据库中的实际记录中,猫鼬也不在乎,只会显示您为对象/架构定义的内容。

Thanks to everyone for your efforts, if nothing else, you helped me get to my answer. 感谢大家的努力,如果没有其他事情,您帮助了我。

posting as an answer to allow for better code formatting. 发布作为答案,以实现更好的代码格式。

I have just got home and thrown together a test app to see if I can duplicate your error. 我刚到家,就把一个测试应用程序放在一起,看看我是否可以复制您的错误。 So far I haven't been able to. 到目前为止,我还没有做到。 Here are my files 这是我的档案

Note: I am not using a database for testing so the issue could be how you get the data from the database into the template: 注意:我没有使用数据库进行测试,因此问题可能是如何将数据库中的数据获取到模板中:

index.jade 玉器

ul
  each sub in subscribers
    li= sub.email

routes/index.js 路线/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 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);

Could you post your routes / app.get function will all the database handling code as well? 您能否发布您的route / app.get函数,所有数据库处理代码也会一样吗?

The jade looks right to me, perhaps it's something in your request handler with how you're adding it to the view to be rendered. 翡翠对我来说似乎不错,也许这是您的请求处理程序中有关如何将其添加到要渲染的视图中的东西。 This simple example works: 这个简单的例子起作用:

server side request handler... 服务器端请求处理程序...

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 });
});

contents of index.jade... index.jade的内容...

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

that outputs... 输出...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM