我的mongoose函数忽略了promises查询并立即下降到结束,而没有等待其他步骤
exports.editProduct = (id, data) => {
Product.findById(id)
.then((data) => {
var imgS3arr = data.img;
var tempArr = [];
var i = imgS3arr.length
while (i--) {
if (!imgS3arr[i].match(/https:\/\/s3.eu-central-1.amazonaws.com\/es-shop\//g)) {
tempArr.push(imgS3arr[i])
imgS3arr.splice(i, 1)
}
}
return tempArr
})
.then((tempArr) => {
var tempArrS3 = []
return Promise.all(tempArr.map((img, i) => {
return fetch.remote(img).then((base) => {
var buf = Buffer.from(base[0], 'base64');
var imgS3 = {
Key: data.title.replace(/( )|(")/g, "_") + "_" + Math.random().toString(36).substring(2),
Body: buf,
ContentEncoding: 'base64',
ContentType: 'image/jpeg'
};
return s3Bucket.putObject(imgS3).promise().then((data) => {
tempArrS3.push('https://s3.eu-central-1.amazonaws.com/es-shop/' + imgS3.Key)
console.log(tempArrS3)
}).catch((err) => {
throw err;
});
});
}))
.then((tempArrS3) => {
edited.title = data.title;
edited.img = imgS3arr.concat(tempArrS3);
return edited
});
})
.then((edited) => {
console.log(edited)
return edited.save();
});
}
There is a point where I call this function
我认为,我使用承诺不正确
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
您忘记在return
函数中调用editProduct
语句:
exports.editProduct = (id, data) => {
return Product.findById(id);
...