【问题标题】:Firestore snapshot properties undefined in cloud functions云函数中未定义的 Firestore 快照属性
【发布时间】:2020-10-30 08:09:45
【问题描述】:

我正在编写一个小函数来发送基于使用多个通配符的文档创建的电子邮件。

奇怪的是,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]);
    });

信息:

firebase 功能 3.11.0 firebase-admin 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

【问题讨论】:

  • 我已经尝试了相同的设置,但没有任何问题,并且能够访问创建的文档的值。我想到的只是“FullEvent”界面中存在一些问题。你能发布它的代码吗?
  • @MethkalKhalawi 我更新了代码。我会再次尝试不使用该类型(我一开始就这样做了)。
  • 是的,我明白了。我想告诉你试试看:fullEvent.fullEvent.email :)
  • 我认为最好将它留给其他人在一些故障排除步骤中提供帮助。
  • 刚刚发布了一个答案。随意接受它

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


【解决方案1】:

我们可以从提供的日志中看到:

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);

根据您提供的代码。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2019-02-18
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2021-05-02
    • 2019-03-18
    • 2021-02-27
    相关资源
    最近更新 更多