Trying to return a string value from a function and save it to mongoDB doesn't work
It's not a bug. It's a feature. JavaScript IO is asynchronous. Your best bet is to make url
return a promise, so you can take advantage of async/await
. MinIOClient
methods return a promise f no callback is passed, so use that.
// If a custom image was selected by the client set the picture URL to Firebase's CDNconst url = async () => { // If the user has selected a file if (req.file) { // Upload user image to the database await MinIOClient.fPutObject("local", `${req.body.email}.png`, req.file.path, { "Content-Type": req.file.mimetype }); // Getting the link for the object const presignedUrl = await MinIOClient.presignedGetObject("local", `${req.body.email}.png`, 24 * 60 * 60) return presignedUrl; } // If the user didn't select an image return a random image link(string) that will be used to serve default avatars from the server else { return avatarLinks[Math.floor(Math.random() * avatarLinks.length)]; }};
Then modify the controller
router.post("/", upload.single("avatar"), async (req, res) => { const pictureUrl = await url(); let Account = new AccountSchema({ // ... other values pictureUrl });});