gpt4 book ai didi

node.js - 我的 Mongoose 模式不正确吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:49 25 4
gpt4 key购买 nike

我相信我的 Mongoose Schema 一定是不正确的,因为当我在 Mongoose 中使用 .save() 时,我的数据库没有正确更新。下面的nodeJS路由运行后,我在回调中收到的更新后的对象是正确的,但在后续运行中,数据库对象为空,并且从未更新。

以下是我的数据的示例:

      var newApplicationData = new ApplicationData({
pos1: 0,
pos2: 0,
applicationData: [

// Standard One
{
sections: [
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
}
]
},

// Standard 2
{
sections: [
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
}
]
},

// Standard 3
{
sections: [
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
}
]
},

// Standard 4
{
sections: [
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
},
{
narrative: '',
documentation: []
}
]
},
]
});

现在这是我的 Mongoose 模式:

const mongoose = require('mongoose');

const ApplicationDataSchema = mongoose.Schema({
pos1: {
type: Number,
required: true,
},
pos2: {
type: Number,
required: true,
},
applicationData: {
type: Array,
required: true,

sections: {
type: Array,
required: true,

narrative: {
type: String,
required: true
},
documentation: {
type: Array,
required: true
}
}
}
}, { collection : 'applicationData' });

const ApplicationData = module.exports = mongoose.model('ApplicationData', ApplicationDataSchema);

最后,这是我尝试使用 .save() 保存数据的 NodeJS 路线:

app.post('/upload', multer({ storage: storage }).single('file'), (req, res) => {

if(!req.file) return res.send({ status: 400, msg: 'No File Received.' });

let pos1 = req.body.pos1;
let pos2 = req.body.pos2;

ApplicationData.findById(mongoose.Types.ObjectId(req.body.applicationDataID), (err, applicationDataObject) => {

if(err) return res.send({ status: 400, msg: err });

applicationDataObject.applicationData[pos1].sections[pos2].documentation.push(req.file.filename);

applicationDataObject.save((err, updatedApplicationData) => {

console.log(applicationDataObject.applicationData[pos1].sections[pos2].documentation);

if(err) return res.send({ status: 400, msg: err });

return res.send({ status: 200, applicationData: updatedApplicationData.applicationData });
});
});
});

最佳答案

当您使用对象数组时,请将对象描述放入类型

      {
pos1: {
type: Number,
required: true,
},
pos2: {
type: Number,
required: true,
},
applicationData: {
type: [{
sections: {
type: [{
narrative: {
type: String,
required: true,
},
documentation: {
type: Array,
required: true,
},
}],
required: true,
},
}],
required: true,
},
}
<小时/>

最好是使用子模式,例如:

const subSchema2 = new mongoose.Schema({
narrative: {
type: String,
required: true,
},
documentation: {
type: Array,
required: true,
},
})

const subSchema1 = new mongoose.Schema({
sections: {
type: [subSchema2],
required: true,
},
})

const finalSchema = new mongoose.Schema({
pos1: {
type: Number,
required: true,
},
pos2: {
type: Number,
required: true,
},
applicationData: {
type: [subSchema1],
required: true,
},
})

关于node.js - 我的 Mongoose 模式不正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078549/

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