更新されたドキュメントをロードする「onUpdate」関数を作成しようとしています。次に、ワイルドカードによって受け取ったデータを使用して、別のドキュメントをロードしたいと考えています。要約すると、更新されたドキュメントと同じコレクションにある別のドキュメントにアクセスしたいと思います。
私は欲しい:/userProfiles/{doc1}/employees/{doc2}
そして/userProfiles/{doc1}
。
両方を取得できますが、一方のデータを使用しようとすると、前のデータが読み取られず、ReferenceError
.
最終的な目標は、これらの両方のドキュメントを使用して、nodemailer で電子メールを送信することです。助けてくれてありがとう。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
admin.initializeApp();
exports.testLog = functions.firestore
.document('/userProfiles/{doc1}/employees/{doc2}')
.onUpdate((change, context) => {
var info = [];
const doc1 = context.params.doc1;
const doc2 = context.params.doc2;
const db = admin.firestore();
return (
db
.collection("userProfiles")
.doc(`${doc1}`)
.get()
.then(doc => {
var email = doc.data().email;
var phone = doc.data().phone;
info.push(doc.data());
console.log(email, phone); // sees and gets info
return email, phone;
}),
db
.collection("userProfiles")
.doc(`${doc1}`)
.collection(`employees`)
.doc(`${doc2}`)
.get()
.then(doc => {
info.push(doc.data());
var Status = doc.data().Status;
console.log(phone, `${Status}`); //phone is undefined
if (`${Status}` === "Alarm") {
// replace with variables from the users settings page
console.log(`${info.phone}`); // phone is undefined
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: "xxxxxx@gmail.com",
pass: "xxxxxxxxxx"
}
});
// send mail with defined transport object
let mailOptions = {
from: '"Fred Foo " <foo@example.com>',
to: `${info.phone}`, // tried phone as well
subject: "Hello ✔",
text: "216+?",
};
transporter.sendMail(mailOptions, error => {
if (error) {
return console.log(error);
} else {
return console.log("message sent");
}
});
}
console.log(Status);
// return
return console.log("im after the if statement. No alarm triggered");
})
.then(message => console.log(message.sid, "success"))
.catch(err => console.log(err))
);
});
したがって、これら2つの画像で電話番号とステータスを取得したい 返されるエラー:
ReferenceError: 電話が定義されていません