繁体   English   中英

尝试部署我的后端 Firebase 功能时承诺出错

[英]error in promises while trying to deploy my backend firebase functions

我第一次使用 firebase 作为我的服务器和数据库,并且我正在尝试将我的 firebase 后端功能部署到 firebase 我在我的控制台中不断收到一个关于没有嵌套我的承诺的错误:

"error 每个 then() 都应该返回一个值或者抛出 promise/always-return
52:16 警告避免嵌套承诺承诺/无嵌套
52:16 警告避免嵌套承诺承诺/无嵌套”

任何其他方式来写这个承诺?

let Promise = require('promise');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.addSimilarImages = 
functions.firestore.document('photos/{document}')

 .onCreate((snap, context) => {

       console.log('SNAP', snap)
       console.log('CONTEXT', context) 


by recreating a google storage style url called photoUrl
       const data = snap.data();
       console.log('DATA IN IS', data)
       const photoUrl = "gs://" + data.bucket + "/" + data.fullPath;
       conolse.log('url is', photoUrl);
 return Promise.resolve()
        .then(() => {
                //we put the photoUrl through the vison API and it returns a list of similar images 
               return visionClient.webDetection(photoUrl);

        })  //place these similar images in a array 
         .then(results => {
               console.log('VISION data all is: ', results)
               const webDetection = results[0].webDetection 

                 //update the document in the photos collection with the similarImages images 
               let similarImages = [];
               if (webDetection.visuallySimilarImages.length) {

webDetection.visuallySimilarImages.forEach(image => {
                               similarImages.push(image);
                       });
               }

                console.log('similarImages', similarImages)

db.collection('photos').doc(context.params.document).update({ 
similarImages })

         })
              .catch(err => console.log(err));
 }) 
   .then(res => console.log('pictures added'))
   .catch(err => console.log(err));

一开始你做了一件好事:

    return visionClient.webDetection(photoUrl); //Returns a promise
})  //place these similar images in a array 
  .then( /* ... */ //Work on the promise

但最终,在你的承诺中,你只是做

db.collection('photos').doc(context.params.document).update({ 
similarImages })
               .then(res => console.log('pictures added')) //Promise in promise
               .catch(err => console.log(err));//Promise in promise

         }) //End of promise

当你可以做

    return db.collection('photos').doc(context.params.document).update({ 
similarImages })
   }) //End of promise
   .then(res => console.log('pictures added')) //Promise outside of promise
   .catch(err => console.log(err));//Promise outside of promise

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM