云功能错误 - '函数返回未定义,预期的承诺或值'

时间:2018-01-08 15:04:37

标签: javascript function firebase cloud

我有一个云功能,我正在发送电子邮件。但是,当我发送电子邮件时,我收到了错误消息。电子邮件发送成功但是在控制台中显示错误“函数返回未定义,预期的Promise或值”。 我已经尝试过几次调整脚本,但是我有点在努力解决错误的来源。我在下面添加了我的代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport 
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
    service: 'gmail',
    auth: {
     user: gmailEmail,
     pass: gmailPassword
   }
});

exports.checkForFails = functions.firestore
   .document('equipment/{itemId}')
   .onUpdate(event => {
         newDoc = event.data.data();
         if(newDoc.hasFailed){
             const mailOptions = {
                 from: '"Emailer" <noreply@firebase.com>',
                 to: "desination@email.com"
             };

             const userDatabase = admin.firestore().doc('users/' +  
             newDoc.user);

             userDatabase.get().then(function(snap){
                 data = snap.data();
                 mailOptions.subject = ('Text Subject' + data.subject);
                 mailOptions.text = ('Text body' + data.body);
             })
             .then(() => {
                  mailTransport.sendMail(mailOptions)
                  .then(() => console.log('Notification sent to WFE email'))
                  .catch(error => console.log('There was an error'))
             })
             .catch(error => console.log('Couldnt send mail', + error))
        }else {
             return 0;
        }
   })

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

如果您的功能执行异步工作,it needs to return a promise在该工作完成时已解决。

您应该返回userDatabase.get().then(...).then(...).catch(...)生成的承诺。