快照为某些子项返回空值,但其他子项不返回

时间:2019-07-15 15:47:31

标签: javascript node.js firebase-realtime-database nodemailer

将Firebase的实时数据库与Google Cloud Functions结合使用,并具有戏剧性的理解,这些代码在哪里出错。当它拉回数据时,其中的一些将被返回,而其他则不会。

Firebase层次结构 enter image description here

Nodemailer代码(node.js):

exports.onEmailRequest = functions.database.ref("emailReq/{pushId}").onCreate((snapshot,context)=>{
    var emailAddress = snapshot.child("email").val();
    const object = snapshot.val().toString();
    console.log("object: "+snapshot);
    var id = context.params.pushId;
    const type = snapshot.child("appType").val();
    console.log("pushId: "+id);
    const appId = snapshot.child("appliId").val();
    console.log("type: "+type);
    var downloadLink = snapshot.child("link").val();
    var mailOptions = {
        from: "abc@gmail.com",
        to: emailAddress,
        subject: "Your  "+type,
        html: '<p>Please see the attached file .</p>',
        attachments: [{ 
        href: downloadLink, //This needs to be the link to the form, or the actual form
        contentType:"application/pdf",
        filename:"application.pdf"
        }]
    }
    if(type ==="Certificate"){
        var adminDb = admin.database().ref(`historicApps/${appId}/certDownload`);
        adminDb.set(0);

    }else{
        var adminDb2 = admin.database().ref(`historicApps/${appId}/appDownload`);
        adminDb2.set(0);
    }

    return transporter.sendMail(mailOptions).then((info)=>{
        var email = admin.database().ref(`emailReq/${id}`);
        return console.log(info);
    }).catch((error)=> console.log("Error sending email ----",error));

    })

控制台结果: enter image description here

问题: 它返回电子邮件和链接的值,但不返回appType或appliId的值,它们都返回为空值。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试直接从snapshot.val()检索每个属性一次:

exports.onEmailRequest = functions.database.ref("emailReq/{pushId}").onCreate((snapshot,context)=> {
    const { email, appType, appliId, link } = snapshot.val();

    console.log(appType);
    console.log(appliId);
    ...
}