在 Jade 中迭代 Mongoose 属性
我试图从 Mongoose 对象输出属性列表,但我也得到了很多 Javascript 辅助函数。我想知道是否有一种干净的方法来输出我的 Mongoose 架构属性。
我的 Jade 显示代码如下所示:
h4 Legacy data
ul
- each val, key in d.old
li= key + ": " + val
我的 Mongoose 架构定义是
Entry = new Schema({
old : {
submitter : String,
table : String,
wordid : Number
}
});
但是当呈现页面时,还会同时输出一堆其他 Javascript 属性和函数。例如,
_scope: [object Object]
toObject: function () { return this.get(path); }
wordid: 2035
...
有没有一种简单的方法可以迭代我的架构中的属性? 我可以使用指定的列表,但我想知道是否有更好的方法。
实际上,我该怎么写指定的方式呢?在 ruby 中,我知道我可以做 [ 'wordid', 'submitter' ].each
但 Jade 中有等效的吗?
I'm trying to output a list of properties from a Mongoose object, but I get a lot of Javascript helper functions too. I'm wondering if there's a clean way to just output my Mongoose schema properties.
My Jade display code looks like:
h4 Legacy data
ul
- each val, key in d.old
li= key + ": " + val
And my Mongoose schema definition is
Entry = new Schema({
old : {
submitter : String,
table : String,
wordid : Number
}
});
But when the page is rendered, there are a bunch of other Javascript properties and functions that get outputted at the same time. e.g.
_scope: [object Object]
toObject: function () { return this.get(path); }
wordid: 2035
...
Is there an easy way to iterate just through the properties from my schema?
I could use a specified list but I was wondering if there was a nicer way.
Actually, how would I write the specified way? In ruby I know I could do [ 'wordid', 'submitter' ].each
but is there an equivalent in Jade?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在遇到对象的原型属性。您可以使用
.hasOwnProperty
过滤掉它们You're encountering the object's prototype properties. You can filter them out with
.hasOwnProperty
请记住,您还可以在文档上使用方法
toJSON
(mongoose Document#toJSON 的 doc)来获取可在模板中使用的干净 JSON 对象(无需担心 mongoose 文档的内部结构和方法)。事实上,您提到的toObject
方法类似于toJSON
,您可能想要 检查一下。例如:
Remember that you can also use the method
toJSON
on the document (mongoose doc of Document#toJSON) to get a clean JSON object that can be used in your templates (without worrying about mongoose document's internals and methods). In fact, thetoObject
method you mentioned is similar totoJSON
, you might wanna check it out.For example: