我使用猫鼬作为我的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' }]
由于某种原因,subeventIds
在for (const
循环的末尾为空。知道发生了什么事吗?