在 Jade 中迭代 Mongoose 属性

发布于 2024-12-02 16:00:43 字数 712 浏览 0 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单调的奢华 2024-12-09 16:00:43

您正在遇到对象的原型属性。您可以使用 .hasOwnProperty 过滤掉它们

- each val, key in d.old
- if(d.old.hasOwnProperty(key))
    li= key + ": " + val

You're encountering the object's prototype properties. You can filter them out with .hasOwnProperty

- each val, key in d.old
- if(d.old.hasOwnProperty(key))
    li= key + ": " + val
怎言笑 2024-12-09 16:00:43

请记住,您还可以在文档上使用方法 toJSON (mongoose Document#toJSON 的 doc)来获取可在模板中使用的干净 JSON 对象(无需担心 mongoose 文档的内部结构和方法)。事实上,您提到的 toObject 方法类似于 toJSON,您可能想要 检查一下

例如:

doc = EntryModel({old: {submitter: "s", table: "tableS", wordid: "666"}})
console.log(b.toJSON())
// outputs:
{
    "_id": "51fea037434b242816000002",
    "old": {
        "submitter": "s",
        "table": "tableS",
        "wordid": 666
    }
}
// Is a plain JSON object without any other property or method

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, the toObject method you mentioned is similar to toJSON, you might wanna check it out.

For example:

doc = EntryModel({old: {submitter: "s", table: "tableS", wordid: "666"}})
console.log(b.toJSON())
// outputs:
{
    "_id": "51fea037434b242816000002",
    "old": {
        "submitter": "s",
        "table": "tableS",
        "wordid": 666
    }
}
// Is a plain JSON object without any other property or method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文