在mongoDB和Schema中保存数据的方式如下:>
var StatusSchema = new mongoose.Schema({
empName: { projectName: { type: String }, clientName: { type: String }, statusLastWeek: { type: String }, statusThisweek: { type: String }, planNextWeek: { type: String } }
});
var Status = mongoose.model('Status', StatusSchema); module.exports = Status; Description: Want save data in MongoDB, data schema is like above mentioned, save saving data is sored loke as bellow. Inside Mongo DB : { "_id" : ObjectId("5d92f4aba4695e2dd90ab438"), "__v" : 0 } { "_id" : ObjectId("5d92f4b4a4695e2dd90ab439"), "__v" : 0 }
MongoDB中的预期集合:
Dave Smith { projectName: BLE Mesh, clientName: Tera, statusLastWeek: BLE Scan, statusThisweek: BLE List View, planNextWeek: Mqtt config }
Node JS代码:
router.post('/ update',(req,res,next)=> {
userStatus = new wkStatus(req.body) userStatus.save() .then(status => { res.redirect('/success'); console.log ("Status saved in DB") }) .catch(err => console.log(err)) // return next;
});
var StatusSchema = new mongoose.Schema({empName:{projectName:{type:String},clientName:{type:String},statusLastWeek:{type:String},statusThisweek:{type:String},...] >
回答如下:const wkStatus = new wkStatus({ _id: new mongoose.Types.ObjectId(), projectName: req.body.projectName, clientName: req.body.clientName, statusThisweek: req.statusThisweek, statusLastWeek: req.statusLastWeek, planNextWeek: req.planNextWeek }) Status .save() .then(result => { res.status(201).json({ message: "Data Created Successfully", }) console.log(result) // show the response }) .catch(err => { res.status(500).json({error:err}) })
尝试这种方式,希望它能起作用。如果需要更多可以给我发消息>