无法通过“ findById”和“ save()”来更新文档

时间:2018-07-21 18:06:56

标签: javascript node.js mongodb mongoose promise

我正在构建一个电影应用程序。通过猫鼬使用Node.js和mongodb

当用户下订单时,我需要更新“ Show”模型并将已订购的最新座位添加到“ 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“保存”后: enter image description here

有人知道什么问题吗?

谢谢

2 个答案:

答案 0 :(得分:2)

将考虑将操作分为可管理的块。在这种情况下,您想使用来自showTakenSeats 新命令。

首先,对您的快递路线使用异步等待,您需要保存订单并获取创建的订单文档。使用新的创建文档 就座,然后使用 findByIdAndUpdate 方法更新显示文档。

以下示例描述了上述内容:

const express = require('express');
const router = express.Router();

const Order = require('../models/order.js');
const Show = require('../models/show.js');

router.post('/', async (req, res, next) => {
    try {
        /* create a new Order */
        const order = new Order(req.body);
        const newOrder = await order.save();

        /* create a document to use in the update with the following data structure:
            {
                'showTakenSeats.6-0': 5b53735ef7ce3d2cd4bbfee7,
                'showTakenSeats.6-1': 5b53735ef7ce3d2cd4bbfee7,
                'showTakenSeats.6-2': 5b53735ef7ce3d2cd4bbfee7 
            }

            Use the native reduce method on the array to create this 
        */
        const updatedSeats = newOrder.ticketPositions.reduce((acc, position) => {
            acc[`showTakenSeats.${position.join('-')}`] = newOrder._id;
            return acc;
        }, {});

        /* update the show document's embedded showTakenSeats 
           with the new properties from above 
        */
        const updateShow = await Show.findByIdAndUpdate(req.body._ShowId,
            { '$set': updatedSeats },
            { 'new': true }
        );

        res.json(updateShow);

    } catch (e) {
        /* this will eventually be handled by your error handling middleware */
        next(e);
    }
});

答案 1 :(得分:1)

您似乎认为函数按顺序执行,但是当您按.then之类的承诺调用updateShow.save().then时,函数的执行不会等待并继续!这会导致以下错误消息,在您尝试将headers设置为已发送后,您尝试在其中进行设置:

> (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.

您可以进行函数asyncawait的函数调用以完成工作,然后再继续。

示例:

router.post("/", async (req, res) => {
    let order = new Order(req.body);
    await order.save(); // if this fails an exception is thrown
    console.log('order is saved') // statement is executed after order is saved
}

有关asyncawait的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

这可能不是代码中的唯一错误,因此无法回答您的问题,但是这段文本太长,无法发表评论。