0
那么,为什么是对象的变量在调试器 为什么console.log()和调试器中的对象值不同?
和控制台日志有什么不同?我在询问特别是关于_id
这个看起来非常不同的变量!
在调试器中,我找不到这部分:"597e874b52fba6324c9ac192"
!
[ { _id: 597e874b52fba6324c9ac192,
title: 'x',
author: 'z',
body: 'y',
date: 2017-07-31T01:26:35.533Z,
comments: [],
__v: 0 },
{ _id: 597e87660726ab322c303a8e,
title: 'x',
author: 'z',
body: 'y',
date: 2017-07-31T01:27:02.266Z,
comments: [],
__v: 0 },
{ _id: 597e8773c45264303c3fcf0b,
title: 'x',
...
这里是节点JS代码我使用:
function test() {
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/test");
var Schema = mongoose.Schema;
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{body: String, date: Date}],
date: {type: Date, default: Date.now},
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
var Blog = mongoose.model('Blog', blogSchema);
Blog.create({title: 'x', author: "z", body: "y"}, function (err, small)
{
if (err) return handleError(err);
// saved!
});
var weirdResults;
Blog.aggregate([
{$match: {author: {$exists: true}}}
], function (err, results) {
weirdResults = results;
if (err) return next(err);
console.log(weirdResults);
});
}
test();
谢谢大家!在调试器的输出始终ObjectID`:
获得ID说'_id。还有一个小箭头来“展开”,这将向您显示价值。就像任何JavaScript对象一样。 'console.log()'基本上调用'.toString()'原型并且“串化”对象内容。这就是为什么一个人直接显示价值,另一个需要你“展开”对象以检查价值。 –
@NeilLunn如果我花费箭头,甚至没有接近控制台中显示的'_id' – Emilio
@Emilio那么扩展它时会显示什么?但是,是的,mongo对象id是[相当不可思议的](https://stackoverflow.com/questions/13104690/nodejs-mongodb-object-id-to-string) – Bergi