gpt4 book ai didi

json - util.inspect/JSON.stringify 不适用于对象中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:48 35 4
gpt4 key购买 nike

我正在使用各种命令、find、findones、findoneandupdates 等从 mongodb 检索文档。其中大多数命令将返回保存的整个文档,这是我确实想要的。在执行 find 或 findOne Mongoose 时允许使用人口。我试图传回传递给我的整个对象或查看整个对象,但数组出现错误。我仍然可以手动访问这些值,但我想要整个对象的字符串等效项。我尝试过 JSON.stringify 和 util.inspect()。有人可以解决这个问题吗?我看过一些旧帖子解决这个问题。如果问题是我如何存储信息,我将发布我的模型以及有关更改的有用建议,我们将不胜感激。

型号:第一个产品,第二个机器:

var productSchema = new Schema({
productName: String,
produtDesc: String,
author: String,
lastUpdate: { type: Date, default: Date.now },
minimum: { type: Schema.Types.ObjectId, ref: 'Machine' },
recommended: { type: Schema.Types.ObjectId, ref: 'Machine' }
});

var machineSchema = new Schema({
cpu: {
speed: String,
mfg: [String],
family: [String],
cores: Number,
architecture: String
},
memory: {
total: Number,
free: Number
},
storage: [ {
drive: String,
total: Number,
free: Number
} ],
os: {
family: String,
version: String,
uptime: Number
},
screen: {
height: Number,
width: Number
},
browser: {
family: String,
version: String
},
network: [ {
name: String,
speed: Number,
addresses: [ {
address: String,
type: String,
internal: Boolean
} ]
} ],
software: {
".net": [String],
java: [String],
acrobat: String
}
});

输出代码:

result.populate('recommended')
.populate('minimum')
.exec( function(err, done) {
console.log(util.inspect(done,
{ showHidden: false, depth: 10 }));
console.log(done.recommended.network[0]);
reply(util.inspect(done,
{ showHidden: false, depth: null }));
});

示例输出:第一个 util.inspect,第二个 JSON.stringify

// util.inspect
{ _id: 553dc1d60062ee501b88535d,
recommended:
{ _id: 553dc1d60062ee501b88535b,
__v: 0,
software: { acrobat: '9.0', java: [Object], '': [Object] },
network: [ [Object] ],
browser: { version: '9', family: 'Internet Explorer' },
screen: { width: 1920, height: 1080 },
os: { version: '6.1', family: 'Windows_NT' },
storage: [],
memory: { total: 3072 },
cpu: { cores: 4, speed: '2800', family: [Object], mfg: [Object] } },
minimum:
{ _id: 553dc1d60062ee501b885359,
__v: 0,
software: { acrobat: '9.0', java: [Object], '': [Object] },
network: [ [Object] ],
browser: { version: '9', family: 'Internet Explorer' },
screen: { width: 1024, height: 768 },
os: { version: '6.0', family: 'Windows_NT' },
storage: [],
memory: { total: 2048 },
cpu: { cores: 2, speed: '2666', family: [], mfg: [Object] } },
productName: 'Product Name',
author: 'chris.lohl',
__v: 0,
lastUpdate: Fri Apr 17 2015 07:01:37 GMT-0700 (US Mountain Standard Time)

}

// JSON.stringify
'{"_id":"553dc1d60062ee501b88535d","recommended":{"_id":"553dc1d60062ee501b88535b","__v":0,"software":{"acrobat":"9.0","java": ["1.6.0.24"],"":{"net":["2.0","3.5"]}},"network": [{"name":"ethernet","speed":1000,"_id":"553dc1d60062ee501b88535c","addresses":["[object Object]","[object Object]"]}],"browser": {"version":"9","family":"Internet Explorer"},"screen": {"width":1920,"height":1080},"os":{"version":"6.1","family":"Windows_NT"},"storage":[],"memory":{"total":3072},"cpu":{"cores":4,"speed":"2800","family":["i3","i5","i7"],"mfg":["intel"]}},"minimum":{"_id":"553dc1d60062ee501b885359","__v":0,"software":{"acrobat":"9.0","java":["1.6.0.2
4"],"":{"net":["2.0","3.5"]}},"network":[{"name":"ethernet","speed":1000,"_id":"
553dc1d60062ee501b88535a","addresses":["[object Object]","[object Object]"]}],"b
rowser":{"version":"9","family":"Internet Explorer"},"screen":{"width":1024,"hei
ght":768},"os":{"version":"6.0","family":"Windows_NT"},"storage":[],"memory":{"t
otal":2048},"cpu":{"cores":2,"speed":"2666","family":[],"mfg":["intel","amd"]}},
"productName":"Product Name","author":"chris.lohl","__v":0,"lastUpdate":"2015-04
-17T14:01:37.000Z"}'

最佳答案

试试这个

JSON.parse(JSON.stringify(addresses.[0]));

关于json - util.inspect/JSON.stringify 不适用于对象中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907553/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com