我要建一个电影院应用程序。 通过猫鼬如何使用Node.js和MongoDB。
当用户发出命令,我需要更新“显示”模型,并添加奉命“showTakenSeats”对象最新的席位。 请注意,我需要的席位添加到“showTakenSeats”,而不是代替所有的对象。
问题是,当我尝试更新和保存的节目 - 一切似乎确定。 但最终它不会成功在数据库中保存。
下面是代码:
const express = require('express');
const router = express.Router();
const Order = require('../models/order.js');
const Show = require('../models/show.js');
router.post("/", (req, res) => {
if (!req.body) {
return res.sendStatus(400);
}
let order = new Order(req.body);
order.save().then(newOrder => {
console.log("Order saved successfully");
Show.findById(req.body._ShowId).then(updateShow => {
console.log('we got the show');
console.log(newOrder.ticketsPositions);
console.log("updateShow before any changes");
console.log(updateShow);
newOrder.ticketsPositions.forEach(element => {
updateShow.showTakenSeats[element[0] + "-" + element[1]] = newOrder._id;
});
console.log("updateShow after adding new seats");
console.log(updateShow);
updateShow.save().then((updateShowSaved) => {
console.log('updated the last order seats at the Show seats map')
console.log(updateShowSaved);
//res.json(updateShowSaved);
}, err => {
console.log(err);
//res.send(err);
});
res.json(updateShow);
}, err => {
console.log(err);
});
res.json(newOrder);
}, err => {
res.send(err);
});
});
module.exports = router;
的console.log:
Order saved successfully
we got the show
[[6,0],[6,1],[6,2]]
updateShow before any changes
{ _id: 5b5203bfcbb3a311c0911b8f,
_MovieId: 5b45faa4a53b0c05f8959262,
_TheaterId: 5b3dfeb217bc8c23f0e7ec3f,
date: '2018-07-22',
time: '22:00',
dateTime: 2018-07-22T19:00:00.000Z,
showTakenSeats:
{ '0-14': 5b52186a7feaac1028ec592f,
'5-0': 5b530e6dc7d3ed280427145a,
'5-1': 5b530e6dc7d3ed280427145a,
'5-2': 5b530e6dc7d3ed280427145a },
__v: 0,
movieInfo: null,
theaterInfo: null,
id: '5b5203bfcbb3a311c0911b8f' }
updateShow after adding new seats
{ _id: 5b5203bfcbb3a311c0911b8f,
_MovieId: 5b45faa4a53b0c05f8959262,
_TheaterId: 5b3dfeb217bc8c23f0e7ec3f,
date: '2018-07-22',
time: '22:00',
dateTime: 2018-07-22T19:00:00.000Z,
showTakenSeats:
{ '0-14': 5b52186a7feaac1028ec592f,
'5-0': 5b530e6dc7d3ed280427145a,
'5-1': 5b530e6dc7d3ed280427145a,
'5-2': 5b530e6dc7d3ed280427145a,
'6-0': 5b53735ef7ce3d2cd4bbfee7,
'6-1': 5b53735ef7ce3d2cd4bbfee7,
'6-2': 5b53735ef7ce3d2cd4bbfee7 },
__v: 0,
movieInfo: null,
theaterInfo: null,
id: '5b5203bfcbb3a311c0911b8f' }
(node:11476) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent.
(node:11476) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
updated the last order seats at the Show seats map
{ _id: 5b5203bfcbb3a311c0911b8f,
_MovieId: 5b45faa4a53b0c05f8959262,
_TheaterId: 5b3dfeb217bc8c23f0e7ec3f,
date: '2018-07-22',
time: '22:00',
dateTime: 2018-07-22T19:00:00.000Z,
showTakenSeats:
{ '0-14': 5b52186a7feaac1028ec592f,
'5-0': 5b530e6dc7d3ed280427145a,
'5-1': 5b530e6dc7d3ed280427145a,
'5-2': 5b530e6dc7d3ed280427145a,
'6-0': 5b53735ef7ce3d2cd4bbfee7,
'6-1': 5b53735ef7ce3d2cd4bbfee7,
'6-2': 5b53735ef7ce3d2cd4bbfee7 },
__v: 0,
movieInfo: null,
theaterInfo: null,
id: '5b5203bfcbb3a311c0911b8f' }
当我检查文件中的DB后,我运行的代码,新的座椅没有被保存在数据库中。
“保存”后,DB:
任何人都知道是什么问题?
谢谢