Shubham Pratik
Shubham Pratik

Reputation: 542

How to add async to google cloud function?

I want to use async and add await in bucket.upload function. I am also using cors in my function. But I am unable to add async to it, because it gives error.

Here is my code:

exports.registerAdminUser =  functions.https.onRequest(( request, response ) => {
    return  cors (request,response, ()=> {
        const body = request.body;
        const profile_pic = body.profile_pic;

        const strings = profile_pic.base64.split(',');
        const b64Data = strings[1];
       
        const contenttype = profile_pic.type;
        const uid = uniqid();
        const uploadName = uid + profile_pic.name
        const fileName = '/tmp/' + profile_pic.name;
        
        fs.writeFileSync(fileName, b64Data, "base64", err => {
            console.log(err);
            return response.status(400).json({ error: err });
          });



        const bucketName = 'my-bucketname.io';
        const options = {
            destination: "/images/admin/" + uploadName,
            metadata: {
              metadata: {
                uploadType: "media",
                contentType: contenttype ,
                firebaseStorageDownloadTokens: uid
              }
            }
          };
        const bucket = storage.bucket(bucketName);
        bucket.upload(fileName,options, (err, file) => {
            if(!err){
          const imageUrl = "https://firebasestorage.googleapis.com/v0/b/" + bucket.name + "/o/" + encodeURIComponent(file.name) + "?alt=media&token=" + uid;
                return response.status(200).json(imageUrl);
            } else {
                return response.send(400).json({
                    message : "Unable to upload the picture",
                    error : err.response.data 
                })
            }
        });
    })
})

Upvotes: 8

Views: 13617

Answers (4)

Bryce Chamberlain
Bryce Chamberlain

Reputation: 535

For my issue, I just had to replace functions.http('helloHttp', (req, res) => { with functions.http('helloHttp', async(req, res) => {.

So add async right before (req, res). That seems to change the function into an async function and then all the await stuff inside the function starts to work.

Hope that helps! Cloud functions are awesome.

Upvotes: 4

smoore4
smoore4

Reputation: 4866

If your Cloud Function has an http trigger, then it would simply be this:

exports.registerAdminUser = async (req, res) => {

Upvotes: 0

Tim Bouma
Tim Bouma

Reputation: 29

Since async/await is an ES2017 feature, you need to add that to your .eslintrc.js:

module.exports = {
    // ...
    "parserOptions": {
        "ecmaVersion": 2017
    },
    // ...
}

Upvotes: 2

Jojo Narte
Jojo Narte

Reputation: 3104

In your package.json use NodeJS engine 8.

By default GCF uses version 6. To be able to use async/await you must change or add below inside package.json

"engines": {
    "node": "8"
  },

Add the async like below

exports.registerAdminUser =  functions.https.onRequest(
  async ( request, response ) => {
      /**/
      await someFunction(/**/)...
  }
);

Upvotes: 12

Related Questions