【问题标题】:Firebase Cloud Function ends before .get() finishesFirebase Cloud Function 在 .get() 完成之前结束
【发布时间】:2019-12-05 14:26:58
【问题描述】:

我正在尝试在 Firestore 中获取()具有特定字段值的所有用户。然后将它们放在一个列表中,以便能够向所有人发送电子邮件。但是由于某种原因,在找到所有电子邮件之前,该功能会一直结束(根据日志)。

我还收到以下错误: 函数返回未定义的、预期的 Promise 或值

这是我的代码:

exports.stuurJuisteTekenaarUpdate =
functions.firestore.document('opdrachten/{opdrachtId}').onCreate((snap, context) => {
 const nieuweOpdracht = snap.data()
 console.log(nieuweOpdracht.disciplines);
 let tekenaarsRef = db.collection('tekenaars');
 let alleTekenaars = tekenaarsRef.where('disciplines', '==', nieuweOpdracht.disciplines).get().then(snapshot => {
      const promises = [];
      snapshot.forEach(doc => {
       const p = doc.data().email
       console.log(p);
       promises.push(p)
      })
    return Promise.all(promises)
  })
  //return null
});

使用最后的“return null”可以使警告静音,但不能解决函数在收到电子邮件地址之前结束的问题。

一旦解决了这个问题,我想运行以下代码:

     .then(emailAdressen => {
      // getting dest email by query string
      const dest = 'vaneijkchris@gmail.com';
      const mailOptions = {
       from: 'CAD-Tekenaar.com <info@cad-tekenaar.com>',
       //to: emailAdressen,
       bcc: emailAdressen,
       subject: `Er is een nieuw tekenproject aangemeld - CDT.${nieuweOpdracht.opdrachtnummerjaar}.${nieuweOpdracht.opdrachtnummer}`, // email subject
       html: `
        <p style="font-size: 16px;font-weight: 600;">NIEUW TEKENPROJECT AANGEMELD</p>

            <div style="height: 2px;background-color: #144b89; width: 100%;margin-bottom: 20px;"></div>


<p style="font-size: 16px;font-weight: 600;">Beste tekenaar,</p>
              <p style="font-size: 16px;">Er is vandaag een nieuwe opdracht binnengekomen binnen jouw vakgebied.<br>Als je interesse hebt wil je mij een mail sturen?<br></p>
              <p style="font-size: 16px;"><br>Discipline: ${nieuweOpdracht.disciplines}</p>
              <p style="font-size: 16px;">Tekenprogramma: ${nieuweOpdracht.tekenprogramma}</p>\
              <p style="font-size: 16px;">Opdrachtomschrijving:<br> ${nieuweOpdracht.omschrijving}</p>\
            <br>
            <p>Interesse in deze opdracht? Reageer dan op deze email.</p>


`
       // email content in HTML
      };

      // returning result
      return transporter.sendMail(mailOptions, (erro, info) => {
       if(erro){
        return res.send(erro.toString());
       }
       return Promise.all(res.send('Sended'));
      });

      return Promise.all()

     })
     .catch(error => {
       console.log(error);
       response.status(500).send(error)
     })

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您没有从函数回调的顶层返回一个承诺。这是必需的,以便 Cloud Functions 知道所有异步工作何时完成并且可以安全清理。试试这个:

     return tekenaarsRef.where('disciplines', '==', nieuweOpdracht.disciplines).get().then(snapshot => {
          const promises = [];
          snapshot.forEach(doc => {
           const p = doc.data().email
           console.log(p);
           promises.push(p)
          })
        return Promise.all(promises)
      })
    

    read the documentation了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多