【问题标题】:Firebase cloud function error in updating collectionFirebase 云功能在更新集合时出错
【发布时间】:2020-06-08 15:13:30
【问题描述】:

我正在尝试使用云功能,这样当在集合“RecruiterJobs”中创建文档时,会触发云功能并更新集合“CollegeCode”的文档中的值,并根据条件单个文档或所有文档收集文件将被更新, 这是我的云函数的 index.js 部分

const functions = require('firebase-functions');
const admin= require('firebase-admin');


exports.TPOjobcount = functions.firestore
    .document('Recruiter/{JobId}')
    .onCreate((snap, context) => {
      const newValue = snap.data();
      const colleges = newValue.Colleges
      if (colleges === 'All'){
       const docRef = admin.firestore().collection('CollegeCode')
                    docRef.get().then(snapshot=>{
                        snapshot.forEach(doc=>{
                            docRef.ref.set({
                                OffCampusJobs:firebase.firestore.FieldValue.increment(1),
                                JobRecruiter: firebase.firestore.FieldValue.arrayUnion(newValue.CompanyName)
                            },{merge:true}).catch(() => null)
                        })
                    }).catch(() => null)
      }
      

当我尝试运行 firebase deploy --only 函数时,它给出了错误

 19:39  error    Each then() should return a value or throw  promise/always-return
  21:29  warning  Avoid nesting promises                      promise/no-nesting
  30:62  error    Each then() should return a value or throw  promise/always-return
  32:17  warning  Avoid nesting promises                      promise/no-nesting

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您需要正确返回异步 Firestore 方法返回的不同承诺的链。

    这在处理 Cloud Functions 中的异步方法时至关重要。有关更多详细信息,我建议您观看 Firebase 视频系列中有关“JavaScript Promises”的 3 个视频:https://firebase.google.com/docs/functions/video-series/


    另外,由于您要并行执行多个对set() 方法的调用,因此需要使用Promise.all()


    以下应该可以解决问题。 但是,由于您使用docRef.ref.set(...)docRefCollectionReference,因此不清楚您到底想写哪个文档(CollectionReference 没有@ 987654330@ 财产)。请根据需要调整您的问题,以防需要进行微调。

    exports.TPOjobcount = functions.firestore
        .document('RecruiterJobs/{JobId}')
        .onCreate((snap, context) => {
    
            const newValue = snap.data();
            const colleges = newValue.Colleges
    
            if (colleges === 'All') {
                const colRef = admin.firestore().collection('CollegeCode');
                return colRef.get()
                    .then(snapshot => {
                        const promises = [];
                        snapshot.forEach(doc => {
                            promises.push(doc.ref.set({
                                OffCampusJobs: firebase.firestore.FieldValue.increment(1),
                                JobRecruiter: firebase.firestore.FieldValue.arrayUnion(newValue.CompanyName)
                            }, { merge: true }))
                        });
                        return Promise.all(promises);
                    })
            }
            else {
                const colRef = admin.firestore().collection('CollegeCode')
                return colRef.where('StudentCode', '==', colleges).get().then(snapshot => {
                    return snapshot.docs[0].ref.set({
                        OffCampusJobs: firebase.firestore.FieldValue.increment(1),
                        JobRecruiter: firebase.firestore.FieldValue.arrayUnion(newValue.CompanyName)
                    }, { merge: true });
                });
            }
    
        });
    

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2023-03-09
      • 2019-09-12
      • 1970-01-01
      • 2019-01-03
      • 2021-01-11
      相关资源
      最近更新 更多