【发布时间】:2019-05-22 09:20:06
【问题描述】:
我想知道 ._doc 在 mongoose 或 mongodb 中的确切含义。以及为什么 destructuring/... 在console.log 中显示错误但在 return 语句中工作正常。
接下来的 2 行返回相同的对象,所以为什么它在那里以及 ._doc 正在做什么
控制台日志(事件); // 返回相同的对象
console.log(event._doc); // 返回相同的对象
`events: () => {
return Event.find()
.then(events => {
return events.map(event => {
console.log(event); // return the same objects
console.log(event._doc); // return the same objects
console.log(...event); /* Graphql say "Found non-callable @@iterator" but later, on return I'm using destructuring without any errors why ? */
return {
...event._doc,
_id: event._doc._id.toString(),
date: new Date(event._doc.date).toISOString(),
creator: user.bind(this, event._doc.creator)
};
})
})`
console.log(event); or console.log(event._doc); 的样子
`{ _id: 5c1c6d928debd345a54de4ce,
title: ' Test',
description: 'Test',
price: 26.99,
date: 2018-12-21T04:35:30.672Z,
creator: 5c1c699bc1f1423c0047d2f1,
__v: 0 }
{ _id: 5c1d8cde6efd7f02832aa2fa,
title: 'Test 2',
description: 'another description',
price: 23.22,
date: 2018-12-22T01:01:18.735Z,
creator: 5c1c699bc1f1423c0047d2f1,
__v: 0 }
{ _id: 5c1d8d6f6f51b802fc32ab22,
title: 'Test 3',
description: 'another description 3',
price: 123.22,
date: 2018-12-22T01:03:43.543Z,
creator: 5c1c699bc1f1423c0047d2f1,
__v: 0 }`
所以它甚至不是一个数组,为什么我需要一个 ... 作为回报?
我在教程中遵循此代码,但不理解这两件事,因此我将非常感谢任何有用的答案。
我正在使用:Mongo, MongoDb and GraphQl
谢谢。
【问题讨论】:
-
毕竟,它是一个内部属性(这就是下划线的意思),你的代码不应该与它交互。
标签: javascript mongodb mongoose graphql