简体   繁体   English

创建新的猫鼬子文档并附加到现有的父文档

[英]Creating new mongoose sub-doc and appending to existing parent doc

I'm building a website with a database using NodeJS, MongoDB, Express, Mongoose etc.我正在使用 NodeJS、MongoDB、Express、Mongoose 等构建一个带有数据库的网站。

  1. I have two schema set up: Events and a sub-doc schema Categories (among others).我设置了两个架构:事件和子文档架构类别(等等)。
  2. The function pulls in array which contains the data needed to create several categories (this bit works) as well as the Event ID appended to the end.该函数拉入数组,其中包含创建多个类别所需的数据(此位有效)以及附加到末尾的事件 ID。
  3. The first few bits below just grab that ID, then remove it from the array (probably a better way to do this, but again, it works).下面的前几位只是获取该 ID,然后将其从数组中删除(这可能是一种更好的方法,但同样有效)。
  4. As mentioned above, the Categories then create correctly (and even do validation), which is amazing, BUT...如上所述,类别然后正确创建(甚至进行验证),这是惊人的,但是......
  5. They don't get appended to the Event doc.它们不会附加到事件文档中。 The doc updates the "categories" field to an applicable number of "null" values, but I cannot for the life of me get it to actually take the IDs of the newly created categories.该文档将“类别”字段更新为适用数量的“空”值,但我一生都无法让它实际获取新创建的类别的 ID。

I nabbed (and adjusted) the below code from somewhere, so this is where I'm at...我从某个地方获取(并调整)了以下代码,所以这就是我所在的位置...

     exports.addCategories = catchAsync(async (req, res, next) => {
       const categories = req.body;
       const length = categories.length;
       const eventID = categories[length - 1].eventId;
       categories.pop();

    Event.findOne({ _id: eventID }, (err, event) => {
      if (err) return res.status(400).send(err);
      if (!event)
        return res.status(400).send(new Error("Could not find that event"));

    Category.create(categories, (err, category) => {
      if (err) return res.status(400).send(err);

      event.categories.push(category._id);
      event.save((err) => {
        if (err) return res.status(400).send(err);

        res.status(200).json(category);
        });
      });
     });
    });

Currently the mongoose debug output is showing the following (which confirms that MOST of it is working, but the IDs just aren't being pulled correctly):目前 mongoose 调试输出显示以下内容(这证实了它的大部分工作正常,但 ID 没有被正确拉出):

> Mongoose: events.updateOne({ _id: ObjectId("614bc221bc067e62e0790875")}, { '$push': { categories: { '$each': [ undefined ] } }, '$inc': { __v: 1 }}, { session: undefined })

Nevermind!没关系! I realised that "category" was still an array, rather than an element of the categories array as I'd assumed.我意识到“类别”仍然是一个数组,而不是我假设的类别数组的一个元素。

So I replaced that section with this, and now... it works!所以我用这个替换了那个部分,现在......它起作用了!

  Category.create(categories, (err, categories) => {
    if (err) return res.status(400).send(err);

    categories.forEach((category) => {
      event.categories.push(category._id);
    });

    event.save((err) => {
      if (err) return res.status(400).send(err);
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM