Node.js 和 JSON.stringify 缺少对象中的一些值/参数

发布于 2024-12-04 00:29:52 字数 697 浏览 1 评论 0原文

也许我不像我想象的那样理解 javascript/coffee 脚本,但是当我这样做时:

that.thing = thing
that.thing.title = "some title"
console.log(that.thing.title)
console.log(JSON.stringify(that.thing)

我得到输出:

一些标题

{"creation_date":"2011-09-09T00:40:03.742Z","_id":"4e6960638ec80519a0000013"}

问题是当我进行字符串化时(以及稍后当函数存在我似乎有其他有趣的问题,我认为这些问题与“那个”有关,并且这个嵌套在多个fxn 呼叫)。

(我现在必须做一个丑陋的解决方案,我这样做。thing = {} 来解决我的问题。之前我必须解决的其他问题包括node.js + async + mongoose.find,而这一切都在 async.findEach 内)

当我这样做时,

console.log(that.thing.toJSON) 

我得到:

function () { return this.toObject(); }

谢谢。

Maybe I don't understand javascript/coffee script as well as I thought but when I do this:

that.thing = thing
that.thing.title = "some title"
console.log(that.thing.title)
console.log(JSON.stringify(that.thing)

I get output:

some title

{"creation_date":"2011-09-09T00:40:03.742Z","_id":"4e6960638ec80519a0000013"}

The problem is I seem to lose the title property when I do the stringify (and later on when the function exists I seem to be having other interesting problems which I assume have to do with 'that' and this nested within multiple fxn calls).

(I had to do an ugly solution for now where I do that.thing = {} to solve my problem. Other problems I had to solve before included node.js + async + mongoose.find and this is all inside async.findEach)

When I do

console.log(that.thing.toJSON) 

I get:

function () { return this.toObject(); }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

心清如水 2024-12-11 00:29:52

这是因为 Model.find() 返回一个 MongooseDocument。如果您想要一个可以添加属性的纯 JavaScript 对象,则需要指定“lean”选项:

Model.find().lean().exec(function (err, thing) {
    thing.title = "some title";
    console.log(JSON.stringify(thing));
});

只要您不需要再次保存事物对象(因为这不是 MongooseDocument,不应用 getter 和 setter)。

This is because Model.find() is returning a MongooseDocument. If you want a plain JavaScript object, which you can add properties to, you need to specify the "lean" option:

Model.find().lean().exec(function (err, thing) {
    thing.title = "some title";
    console.log(JSON.stringify(thing));
});

This will work fine, provided you don't need to save the thing object again (as this is not a MongooseDocument, no getters and setters are applied).

庆幸我还是我 2024-12-11 00:29:52

既然您提到了 mongoose,那么 that.thing 可能是一个具有某些特定属性的特殊对象。

有两件事可能会影响这一点。

要么 thing 实际上是一个带有一些可怕逻辑的 getter/setter

或者更可能的是 thing 有一个 .toJSON 方法将对象写入 json 并且你没有增强或删除该方法来使 JSON.stringify 工作。

我的默认 JSON.stringify 调用 .toJSON 方法(如果存在)。

因此,一个简单的 console.log(that.thing.toJSON); 应该可以确定您的问题

无论如何,当您想要记录数据并确保它记录当前数据的内容时,您真正应该做什么一种阻挡时尚。

console.warn(util.inspect(that.thing));

Since you mention mongoose it may be that that.thing is a special object with some specific properties.

There are two things that could be affecting this.

Either thing is actually a getter/setter with some horrible logic

Or more likely thing has a .toJSON method that writes the object to json and you havn't augmented or removed that method to make JSON.stringify work.

My default JSON.stringify calls the .toJSON method if it exists.

So a simple console.log(that.thing.toJSON); should identify your problem

Anyway what you really aught to be doing when you want to log data and make sure it logs what the current data is in a blocking fashion.

console.warn(util.inspect(that.thing));

花开雨落又逢春i 2024-12-11 00:29:52

您看到“function () { return this.toObject(); }”,因为 toJSON 是一个原型函数。

也就是说,console.log(that.thing.toJSON) 将打印函数本身,但 console.log(that.thing.toJSON()) 将打印从函数返回的值。添加额外的括号将执行该函数。

尝试 console.log(that.thing.toJSON()) 并查看是否得到您正在寻找的内容。

You are seeing "function () { return this.toObject(); }" because toJSON is a prototyped function.

That is, console.log(that.thing.toJSON) will print the function itself but console.log(that.thing.toJSON()) will print the value returned from the function. Adding the extra brackets will execute the function.

Try console.log(that.thing.toJSON()) and see if you get what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文