我正在编写一个小函数,用于在使用多个通配符创建文档的基础上发送电子邮件。
奇怪的是,snapshopt.data((对象在日志中打印属性时拥有所有属性。然而,当我试图访问此对象的属性时,它是未定义的(例如,无法读取未定义的属性"电子邮件"(
为了示例起见,我将使用console.log(它已经过测试(。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
type FullEvent = {
email: string;
date: string;
localSlotFrom: string;
localSlotFull: string;
localDate: string;
login: string;
slotFrom: string;
slotFull: string;
ownerEmail: string;
ownerName: string;
name: string;
}
exports.createEmailTask = functions.firestore.document('/{rootCollection}/eventTypes/{eventTypeColId}/{eventTypeDocId}/fullEvents/{eventDocumentId}')
.onCreate((snap, context) => {
const fullEvent = snap.data() as FullEvent;
// const { email } = snap.data() as FullEvent; // -> email is undefined as well this way
console.log(fullEvent); // will have in logs the full object with defined properties (e.g. { name: "My event name", email: "myemail@example.com" }
console.log('full event email ' + fullEvent.email); // Cannot read property 'email' of undefined
return;
以下的全部功能
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
type FullEvent = {
email: string;
date: string;
localSlotFrom: string;
localSlotFull: string;
localDate: string;
login: string;
slotFrom: string;
slotFull: string;
ownerEmail: string;
ownerName: string;
name: string;
}
exports.createEmailTask = functions.firestore.document('/{rootCollection}/eventTypes/{eventTypeColId}/{eventTypeDocId}/fullEvents/{eventDocumentId}')
.onCreate((snap, context) => {
const { email, ownerEmail, name, ownerName, slotFull, localSlotFull, date, localDate } = snap.data() as FullEvent;
console.log('ownerEmail ' + ownerEmail);
const dateFormatted = new Date(date).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
const localDateFormatted = new Date(localDate).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
const p1 = admin.firestore().collection('mail').add({
to: ownerEmail,
message: {
subject: `A new event has been scheduled with ${name}`,
html: `A new event has been scheduled with ${name} on ${dateFormatted} between ${slotFull}`
}
});
const p2 = admin.firestore().collection('mail').add({
to: email,
message: {
subject: `Event scheduled with ${ownerName} confirmed!`,
html: `The event with ${ownerName} has been confirmed and scheduled for the ${localDateFormatted} between ${localSlotFull}`
}
});
return Promise.all([p1, p2]);
});
供参考:
防火基地功能3.11.0firebase管理员9.3.0
我一直在尝试不同的打印数据的方法。我的假设是,这与承诺有关。
我是不是错过了什么?
更新:
日志:
createEmailTask
{"fullEvent":{"ownerEmail":"brieuc@iamremote.dev","date":"2020-11-03","localSlotFrom":"10:00","login":"johndoe","slotFrom":"16:00","slotFull":"16:00 - 17:00","name":"John Doe","localDate":"2020-11-03","localSlotFull":"10:00 - 11:00","email":"brieuc+johndoe@iamremote.dev","ownerName":"Kurniawan"}}
createEmailTask
full event email undefined
我们可以从提供的日志中看到:
createEmailTask
{"fullEvent":{"ownerEmail":"brieuc@iamremote.dev","date":"2020-11-03","localSlotFrom":"10:00","login":"johndoe","slotFrom":"16:00","slotFull":"16:00 - 17:00","name":"John Doe","localDate":"2020-11-03","localSlotFull":"10:00 - 11:00","email":"brieuc+johndoe@iamremote.dev","ownerName":"Kurniawan"}}
看起来您有一个嵌套的fullEvent对象,它包含了所有其余的数据。因此,要访问您的数据,您只需要:
console.log('full event email ' + fullEvent.fullEvent.email);
根据您提供的代码。