异步等待猫鼬文档的创建

时间:2018-12-11 13:06:44

标签: javascript express promise async-await

我使用猫鼬作为我的ORM。我陷入两个模式之间有关系的异步/等待问题。这是代码-

const { _id: userId } = decodedObject;
      const {
        name,
        description,
        location,
        dates,
        tags,
        subevents,
        image
      } = args;

      const subeventsMap = new Map(JSON.parse(subevents));
      const subeventIds = [];

      const conference = await ConferenceModel.create({
        createdBy: userId,
        name,
        description,
        location,
        dates,
        tags
      });

      for (const values of subeventsMap.values()) {
        await values.forEach(async value => {
          const subevent = await SubeventModel.create({
            title: value.title,
            description: value.description,
            location: value.location,
            startTime: value.startTime,
            endTime: value.endTime,
            type: value.type,
            forDate: value.forDate
          });
          subeventIds.push(subevent._id);
        });
      }

      console.log(subeventIds);

      return '123';

ConferenceModel使用这段代码对SubeventModel的引用

subevents: [{ type: Schema.Types.ObjectId, ref: 'Subevent' }]

由于某种原因,subeventIdsfor (const循环的末尾为空。知道发生了什么事吗?

0 个答案:

没有答案