【问题标题】:Why won't the `email` property of my json object display in my jade template?为什么我的 json 对象的 `email` 属性不会显示在我的玉模板中?
【发布时间】:2012-04-16 06:26:59
【问题描述】:

遇到最奇怪的问题。我无法显示对象的email 属性。我正在使用 Mongoose/Express/Jade。我正在做一个:

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

效果很好,并在 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' }

然后我将它传递给我的翡翠视图中的一个名为订阅者的变量。 到目前为止一切顺利,当我在我的翡翠模板中迭代 subscribers 时,我想显示每个订阅者的 email 属性时出现了问题。出于某种原因,这只是email 的问题! email 没有显示任何内容,只是空白。

ul
  each sub in subscribers
    li= sub.email

如果我用任何其他属性代替email,它就可以正常工作。因此,以下显示完全正常:

ul
  each sub in subscribers
    li= sub.username

如果我能提供更多细节,请告诉我。

为 Declan 编辑:

执行以下操作:

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

【问题讨论】:

  • 如果只是为了测试,你在你的控制器中添加循环并尝试 console.log 其中的每个子,它显示了什么?你的财产在里面吗?
  • 是的,它正在显示,这就是我复制和粘贴整个对象的地方。
  • 我想知道翡翠是否对电子邮件中的特殊字符有问题。您是否尝试过删除 @ 和 .并测试它们是否导致了问题?
  • @DeclanCook 好主意,但不行。我将所有电子邮件帐户值设置为 foo,但问题仍然存在。
  • 很奇怪,你是否厌倦了使用符号:每个 val,key in obj li #{key}: #{val}

标签: javascript node.js express mongoose pug


【解决方案1】:

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

当使用 mongoose 并且某个属性未显示时,请确保您已在 mongoose 架构中定义了该属性。不知何故,我在创建架构时忽略了电子邮件字段,只是没有定义它。即使它在数据库中的实际记录中,猫鼬也不在乎,只会向您显示您为对象/模式定义的内容。

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

【讨论】:

    【解决方案2】:

    作为答案发布以允许更好的代码格式。

    我刚回到家,整理了一个测试应用程序,看看我是否可以复制你的错误。到目前为止,我一直无法。这是我的文件

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

    index.jade

    ul
      each sub in subscribers
        li= sub.email
    

    路由/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);
    

    您能否发布您的路线/app.get 函数以及所有数据库处理代码?

    【讨论】:

    • 我只是控制台记录了数据库的结果,正如预期的那样,所有属性看起来都不错,包括电子邮件。但是,然后我遍历了每条记录中的所有属性,控制台记录了键:值对,所有内容都显示了,除了电子邮件。这是我的路线图:pastie.org/3722045
    【解决方案3】:

    玉对我来说看起来很合适,也许它是您的请求处理程序中的某些内容,以及您如何将它添加到要呈现的视图中。这个简单的例子有效:

    服务器端请求处理程序...

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

    index.jade 的内容...

    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>
    

    【讨论】:

    • 感谢您的测试。我认为这可能与 Mongoose 如何返回对象有关。我不认为它会作为一个实际的数组回来。当我 console.log 来自 Mongoose 的结果(subs)时,我只看到 2 个逗号分隔的对象,就像我在原始帖子中显示的那样。不确定这是对的。
    • @k00k,没错。当您 console.log 一个数组时,它会执行一个 toString,它返回一个以逗号分隔的数组值列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多