Node.js Mongoose检索my document内部数组的null
我有一个集合,其中一个字段是字符串数组。但是当我使用Node.js Mongoose检索my document内部数组的null,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个集合,其中一个字段是字符串数组。但是当我使用find()进行查询时,它返回null,而在MongoDB指南针中我可以看到它 这是我的文件: { "_id":"5d4894f23f86a41b84303795", "position":1, "garmentTitle":"My first Garment", "garmentURL":"www.firstgarment.com", "garmentPictureURL":"/images/first.png"
find()
进行查询时,它返回null
,而在MongoDB指南针中我可以看到它
这是我的文件:
{
"_id":"5d4894f23f86a41b84303795",
"position":1,
"garmentTitle":"My first Garment",
"garmentURL":"www.firstgarment.com",
"garmentPictureURL":"/images/first.png",
"brand":"5d49e60e4eface2a00ac58d7",
"garmentMaker":"5d49e8854eface2a00ac58d9",
"garmentPurpose":"5d49e8754eface2a00ac58d8",
"gender":"5d37546f2f8c280adc60b3fe",
"garmentCategory":"5d3a4c7f447a7a3afc71a746",
"fabricComposition":null,
"garmentSizes":["5d4d211f0fe9591facb9a268"] // <<== This is mentioned array
}
这是我的控制器:
module.exports.getOneGarmentDataByID = function (req, res) {
GD.find({_id:req.params.id})
.exec()
.then(result => {
res.status(200).json(result);
})
.catch(err => {
console.log(err);
res.status(500).json({error:err});
});
}
请求的路线:
http://localhost:3000/gar/getonegarmentdatabyid/5d4894f23f86a41b84303795
最终结果是:
[
{
"garmentSizes": null, <<== returns null!!!
"_id": "5d4894f23f86a41b84303795",
"position": 1,
"garmentTitle": "My first Garment",
"garmentURL": "www.firstgarment.com",
"garmentPictureURL": "/images/first.png",
"brand": "5d49e60e4eface2a00ac58d7",
"garmentMaker": "5d49e8854eface2a00ac58d9",
"garmentPurpose": "5d49e8754eface2a00ac58d8",
"gender": "5d37546f2f8c280adc60b3fe",
"garmentCategory": "5d3a4c7f447a7a3afc71a746",
"__v": 0,
"fabricComposition": null
}
]
[
{
“garmentSizes”:null,架构类型错误。应为:
garmentSizes: {
type: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'GS'
}
],
required: false
}
garmentSizes: {
type: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'GS'
}
],
required: false
}