firestore onUpdate触发器更改参数chnage.after.data和change.before.data是否可能为null或未定义?

问题描述

这是我的云功能

exports.onUpdateEvent = functions.firestore.document('collection/{documentId}')
.onUpdate((change,context) => {
    const after: FirebaseFirestore.DocumentData = change.after.data();
    const before: FirebaseFirestore.DocumentData = change.before.data();
})  

在之后或之前是否可能存在null或undefined?

解决方法

是的,字段可以为空,这种情况可能发生的一种方式是尚不存在要评估的数据,建议在官方documentation中针对可能的空值评估数据,例如:

  // Get an object with the current document value.
  // If the document does not exist,it has been deleted.
  const document = change.after.exists ? change.after.data() : null;