-4

I have written google cloud function, which reads file from the bucket and then return the content.

   async function getFileContent(fileName, bucketName) {
      const storage = new Storage();
      const file = await storage.bucket(bucketName).file(fileName);
      file.download(function(err, contents) {
           console.log("file err: "+err);
           console.log("file data: "+contents); //contents is displaying data here
          return contents;
      });
    }
//cloud function starts
    exports.getdata = async function(req, res) {
      var filecontent = await getFileContent("file1", "bucket1");
      console.log("outside "+filecontent); //displaying as undefined
     ));
    }

I want to execute console.log("outside "+filecontent); only once the getFileContent returns value after processing.

Jax
  • 139
  • 2
  • 12
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CertainPerformance Mar 30 '20 at 22:20
  • You've got an await keyword where you don't need it, and missing await keywords where you do need them. I suggest taking some time to better learn how async/await syntax is meant to work with promises. And be sure to understand how promises are used with the APIs that you're working with. – Doug Stevenson Mar 30 '20 at 22:22

1 Answers1

0

I have done small mistake here, have to keep await for file.download where as in the query i have mentioned above i have given await at storage.bucket(bucketName).file(fileName);

 async function getFileContent(fileName, bucketName) {
      const storage = new Storage();
      const file = storage.bucket(bucketName).file(fileName);
      content = await file.download(function(err, contents) {
           console.log("file err: "+err);
           console.log("file data: "+contents); //contents is displaying data here
          return contents;
      });
    }

the above code is working fine after making this change. Not sure why it is down voted instead of correcting the mistake.

Jax
  • 139
  • 2
  • 12