Ma fonction mangouste ne tient pas compte des promesses requête et tombe instantanément à la fin, sans attendre un autre pasMongoose promesse ne fonctionnent pas
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
Je pense, je l'aide des promesses DonT droit
certains peuvent un m'aider avec ce problème?
Vous devriez avoir soit 'revenir Product.findById (id)' 'ou (id, données) => Product.findById (id)', puisque votre définition de fonction externe ne renvoie pas de Promesse. Il est inutile de renvoyer une promesse du dernier élément de la chaîne si l'appel initial ne renvoie pas. –
S'il vous plaît lorsque vous écrivez du code, en particulier avec javascript. Divisez votre code en fonctions logicielles. L'utilisation de callback/promesses rend très illisible. Vous avez manqué un '.catch' tho, ce qui peut être un problème. –