Reputation: 15
My mongoose function ignores promises query and instantly falling to end, without waiting for another steps
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
I think, I using promises dont right
Can some one help me with this trouble?
Upvotes: 0
Views: 55
Reputation: 19587
You forget to call return
statement in editProduct
function:
exports.editProduct = (id, data) => {
return Product.findById(id);
...
Upvotes: 2