Как добавить асинхронную функцию в Google Cloud? - PullRequest
0 голосов
/ 20 февраля 2019

Я хочу использовать async и добавить await в bucket.upload функцию.Я также использую cors в своей функции.Но я не могу добавить async к нему, потому что он выдает ошибку.

Вот мой код:

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 
                })
            }
        });
    })
})

1 Ответ

0 голосов
/ 20 февраля 2019

В вашем package.json используется NodeJS движок 8.

По умолчанию GCF использует версию 6. Чтобы иметь возможность использовать async/await, вы должны изменить или добавить ниже внутри package.json

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

Добавьте асинхронность, как показано ниже

exports.registerAdminUser =  functions.https.onRequest(
  async ( request, response ) => {
      /**/
      await someFunction(/**/)...
  }
);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...