I want to upload the files to Cloudinary and then get the 'secure_url' s and insert them into a MySQL database. However my current code immediately execute the SQL function. How to make the SQL function wait till the Cloudinary upload finishes.
Below is the code :
app.post('/create', upload.array('uploaded_file'), function (req, res) {
console.log(req)
var wallet = req.body.wallet
var name = req.body.fname
var nic = req.body.nic
var nicfront = req.files[0].path;
var nicback = req.files[1].path;
var selfie = req.files[2].path;
var state = 1
var frontURL = "temp_string"
var backURL = "temp_string"
var selfieURL = "temp_string"
cloudinary.uploader.upload(nicfront, function (err, result) {
console.log("Result: ", result);
frontURL = result.secure_url;
});
cloudinary.uploader.upload(nicback, function (err, result) {
console.log("Result: ", result);
backURL = result.secure_url;
});
cloudinary.uploader.upload(selfie, function (err, result) {
console.log("Result: ", result);
selfieURL = result.secure_url;
});
// Insert into MySQL Database
db.query("INSERT INTO applications (wallet, name, nic, nicfront, nicback, selfie, state) VALUE (?,?,?,?,?,?,?)", [wallet, name, nic, frontURL, backURL, selfieURL, state], (err, result) => {
if (err) {
console.log(err)
} else {
res.send("Inserted into MySQL!")
}
})
});
app.listen(3001, () => {
db.connect(function (err) {
if (err) throw err;
});
});