[英]Mongoose get object in array of objects
I am using mongoose and I am trying to get users from my mongodb database, Here is what part of my json looks like我正在使用 mongoose 并试图从我的 mongodb 数据库中获取用户,这是我的 json 的一部分
"hpCDaKverVWEYukAhAcM8NU6SP73" : {
"admin" : false,
"booksBorrowed" : [ {
"id" : "9780321831552",
"timeStamp" : 1618881802437
}, {
"id" : "9780007204496",
"timeStamp" : 1618881803678
}, {
"id" : "9780316491297",
"timeStamp" : 1618882675513
}, {
"id" : "9780440335160",
"timeStamp" : 1618882676756
}, {
"id" : "9781482287325",
"timeStamp" : 1618887153684
} ],
I am trying to get the books borrowed array我正在尝试借书阵列
i tried creating a new schema like this我尝试像这样创建一个新架构
const BorrowedBook = new Schema({
id : {type: String, default: ''},
timeStamp : {type: Number, default: 0},
})
then doing this in mongoose.model然后在 mongoose.model 中执行此操作
booksBorrowed: [BorrowedBook]
then I do this in server.js然后我在 server.js 中执行此操作
const User = require('./models/user')
app.get('/api/users', function (req, res) {
User.find(function (err, users) {
console.log(users)
})
})
but it just prints this booksBorrowed: [ [Object], [Object], [Object], [Object], [Object] ],
但它只是打印这
booksBorrowed: [ [Object], [Object], [Object], [Object], [Object] ],
What am I doing wrong?我究竟做错了什么?
You are doing nothing wrong, its just console.log(users)
printing this, since it doesnt print nested lvl objects.你没有做错任何事,它只是
console.log(users)
打印这个,因为它不打印嵌套的 lvl 对象。
You can do console.log(JSON.stringify(users))
or console.dir(users)
.您可以执行
console.log(JSON.stringify(users))
或console.dir(users)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.