【问题标题】:error in promises while trying to deploy my backend firebase functions尝试部署我的后端 Firebase 功能时的承诺错误
【发布时间】:2019-10-07 05:19:05
【问题描述】:

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

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

还有其他写这个承诺的方法吗?

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

【问题讨论】:

    标签: javascript firebase promise


    【解决方案1】:

    一开始你做了一件好事:

        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
    
    

    【讨论】:

    • dimitri Mockelyn 我解决了这个问题,谢谢,但现在控制台说“43:16 错误每个 then() 应该返回一个值或抛出 promise/always-return”第二个。然后,不知何故我需要返回值还是抛出?
    • 在承诺中返回一些东西来控制执行流总是更好的。这是一种“最佳实践”。如果这是您呼叫流程的最后一部分,则您不需要。你可以什么都不返回。如果这是一个错误,最好重新抛出它,所以它可以在其他地方处理
    猜你喜欢
    • 2018-07-14
    • 2018-07-12
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2018-04-01
    相关资源
    最近更新 更多