I am creating a function which selects a file name from a MySQL table, then uses the file name to look up an object in Google Cloud storage and retrieve metadata. I need to assign the result of the query to a variable which I can then use in my Google Cloud storage bucket call - but I can't figure out how to do this. Here is what I have:
const File = function (file) {
this.userId = file.userId;
this.fileName = file.fileName;
};
File.getAvi = async (userId, result) => {
const fileName = await sql.query(
`SELECT fileName FROM avatars WHERE userId = ${userId}`,
(err, res) => {
if (err) {
console.log("Error selecting from AVATARS: ", err);
return result(null, err);
}
console.log("Fil Name: ", res);
result(null, res[0].fileName);
}
);
const metadata = await bucket
.file(fileName)
.getMetadata();
return result(null, metadata);
};
But this does not work. The function terminates at the first "result" line at the end of the query and it sends the query result back to the controller, rather than assign the query reulst to const fileName and let me use it in the Google Cloud storage call. How do I assign the query result to a variable which I can use in additional calls?