为什么我的json对象的`email`属性不显示在我的模板中?(Why won't the `email` property of my json object display in my

编程入门 行业动态 更新时间:2024-10-26 22:22:15
为什么我的json对象的`email`属性不显示在我的模板中?(Why won't the `email` property of my json object display in my jade template?)

有最奇怪的问题。 我无法显示对象的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' }

然后我通过一个名为订阅者的var将它传递给我的玉石视图。 所有这一切都很好,当我在我的玉模板中遍历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' }

Having the strangest problem. I can't display the email property of an object. I'm using Mongoose/Express/Jade. 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:

{ _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. 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. It's only a problem with email for some reason! It doesn't show anything for email, just blank.

ul each sub in subscribers li= sub.email

It works fine if I substitute ANY of the other properties in place of 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:

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.

更多推荐

本文发布于:2023-07-26 16:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   模板   email   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!