我正在尝试使用Sequalize ORM在数据库中创建新行。我收到了req.query.collections
的一系列集合。对于每个集合,我需要创建一个新的userCollection
。如果没有创建userCollection
,我想用内部服务器错误(第41行)进行响应,否则返回带有新创建的userCollections
的对象数组。
问题是,当我从Postman发出测试请求时,我不断收到内部服务器错误。当我检查数据库时,发现创建了userCollections
,所以没有发生错误。
我知道为什么会这样:因为userCollection.build({ stuff }).save()
返回了一个承诺。因此,当我尝试从userCollections
语句中进行console.log .then()
时,就像我应该的那样,我得到一个包含新创建的集合的数组。但是到那时,服务器已经响应了内部服务器错误。
这是我的功能代码:
exports.addCollections = async (req, res, next) => {
const libraryId = req.params.libraryId;
const collections = req.query.collections;
if (!collections)
next(Boom.forbidden());
const userCollections = [];
collections.forEach(async (collectionId, index) => {
const collection = await Collection.findByPk(collectionId);
if (!collection)
return next(Boom.notFound());
userCollection.build({
user_id: req.user.id,
library_id: libraryId,
public_collection_id: collection.id,
title: collection.title,
description: collection.description
})
.save()
.then(newUserCollection => {
userCollections.push(newUserCollection.get({ plain: true }));
// should be printed first, but comes second
// prints out the array with newly created record
console.log(userCollections);
})
.catch(error => {
console.log(error);
});
});
// should be printed second, but comes first
// prints out empty array
console.log(userCollections);
if (userCollections.length === 0) {
next(Boom.internal());
}
res.json(userCollections);
}
答案 0 :(得分:0)
发布解决方案
感谢创建本教程的塞巴斯蒂安·肖邦(Sebastien Chopin): https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
所以我添加了此功能:
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
我做的不是collections.forEach...(blah blah)
(问题中发布的代码的第10行),而是:
try {
await asyncForEach(collections, async (collectionId, index) => {
const collection = await Collection.findByPk(collectionId);
if (!collection)
return next(Boom.notFound());
userCollection.build({
user_id: req.user.id,
library_id: libraryId,
public_collection_id: collection.id,
title: collection.title,
description: collection.description
})
.save()
.then(newUserCollection => {
userCollections.push(newUserCollection.get({ plain: true }));
console.log(userCollections);
})
.catch(error => {
console.log(error);
});
})
} catch (err) {
return next(Boom.internal(err.message));
}