My uploadPhoto function should return the id (int) of the photo that has been uploaded.
so inside my main function I've got let photoId = await uploadPhoto(image, name);
here's the code of the uploadPhoto function: First attempt
async function uploadPhoto(image, name) {
try {
let file = fs.readFileSync( image );
await client.uploadFile({
name: image,
type: "image/jpg",
bits: file
}, function( error, data ) {
return data.id
});
} catch (err) {
console.log(err);
}
}
Second attempt
async function uploadPhoto(image, name) {
try {
let file = fs.readFileSync( image );
function uploadF (callback){
client.uploadFile( { name: image, type: "image/jpg", bits: file } , function( error, data ) {
return callback(data.attachment_id);
});
}
let res = await uploadF(function (data){
return data;
});
} catch (err) {
console.log(err);
}
}
client.uploadFilefunction mandates a callback and it seems to call the callback without checking if it exists. When OP left it out, the equivalent ofundefined(null, data)was called which would lead to an error like that.Promiseand usingresolveandrejectinternally, and returning thatPromise.readFileSyncto something that is not sync, likeconst {readFile} = require('fs/promises');+const file = await readFile(image);