Firebase:如何更新在Gen 2 onDocumentCreated触发器事件期间创建的同一文档?

6bc51xsx  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(63)

我无法让触发器更新正在创建的同一个文档。我将调试语句打印到控制台,错误指向documentData.set。我尝试了document.ref.updatedocumentData.ref.setdocumentData.set,但这些都不起作用。控制台在下面显示了这些错误。如何更新在创建触发器期间创建的同一个文档?

document.ref.update:

字符串
TypeError:无法读取未定义的属性(阅读“update”)
documentData.ref.set:
TypeError:无法读取未定义的属性(阅读“set”)
documentData.set:
TypeError:documentData.set不是函数

exports.onFriendAdded = onDocumentCreated(
  "users/{userDocID}/friends/{friendDocID}",
  async (event: any) => {
    const timestamp = getTimestamp();
    const document = event;
    const documentData = document.data.data();
    const moveOnToNextStep = documentData.moveOnToNextStep;
    let friendUID = documentData.uid;

    if (!moveOnToNextStep) {
      return;
    }

    console.log(myAppTag + " onFriendAdded started!");

    // If the friend uid = empty/null, update it
    if (friendUID == "" || friendUID == null) {
      console.log(myAppTag + " 1");
      const friendUsername = documentData.username;
      const friendInfo = await getUserInfo(1, friendUsername, "uid");
      console.log(myAppTag + " 2");

      if (friendInfo[0] == 1) {
        friendUID = friendInfo[2];
        console.log(myAppTag + " 3");

        // If sendFriendRequest = true, send a notification to the friend Doing
        // this here because updating the friendUID causes the onUpdate to fire
        // and don't want to double send the request.
        const sendFriendRequest = documentData.sendFriendRequest;

        if (sendFriendRequest) {
          console.log(myAppTag + " 4");
          const uid = event.params.userDocID;
          let username = "";

          const friendStatus = await getFriendStatus(uid, friendUID);
          console.log(myAppTag + " 5");

          if (friendStatus["returnVal"] == 0) {
            console.log(myAppTag + " 6");
            // If friend stat cant be found, dont send friend request
            console.log(
              myAppTag +
                " onFriendUpdated getFriendStatus returnVal is 0. Error: " +
                friendStatus["returnMsg"]
            );
            documentData.ref.set({
              sendFriendRequest: false,
              modifiedDate: timestamp,
            });
            return;
          }

          if (friendStatus["status"] == "0") {
            console.log(myAppTag + " 7");
            console.log(
              myAppTag +
                `onFriendUpdated getFriendStatus user B has blocked user
                A. status: ` +
                friendStatus["status"]
            );
            documentData.ref.set({
              sendFriendRequest: false,
              modifiedDate: timestamp,
            });
            return;
          }

          const userInfo = await getUserInfo(0, uid, "username");
          console.log(myAppTag + " 8");

          if (userInfo[0] == 1) {
            username = userInfo[2];
          }

          if (username == "" || username == null) {
            console.log(
              myAppTag +
                " onFriendAdded Error when getting user info: " +
                userInfo[1]
            );
            return;
          }
          console.log(myAppTag + " 9");
          const notifResult = await sendNotification(
            uid,
            username,
            friendUID,
            friendUsername,
            notifIDFriendRequest
          );
          console.log(myAppTag + " 10");

          if (notifResult[0] == 1) {
            console.log(myAppTag + " 11");
            documentData.set({ // ERROR POINTS HERE 
              sendFriendRequest: false,
              modifiedDate: timestamp,
            });
            console.log(myAppTag + " 11.1");
          } else {
            console.log(myAppTag + " 12");
            console.error(
              myAppTag +
                " onFriendAdded Error when sending the notification." +
                " uid: " +
                uid +
                " username: " +
                username +
                " friendUID: " +
                friendUID +
                " friendUsername: " +
                friendUsername +
                " returnVal: " +
                notifResult[0] +
                " returnMsg: " +
                notifResult[1]
            );
          }
        }
        console.log(myAppTag + " 12.1");
        documentData.ref.set({
          // Update "document" with the new data
          uid: friendUID,
          modifiedDate: timestamp,
        });
        console.log(myAppTag + " 13");
      } else {
        console.log(
          myAppTag +
            " onFriendAdded Error when getting friend info: " +
            friendInfo[1]
        );
        return; // The friend request (below) will not be sent
      }
    }
    console.log(myAppTag + " 14");
    await documentData.ref.set({
      moveOnToNextStep: false,
      modifiedDate: timestamp,
    });
  }
);

vnjpjtjt

vnjpjtjt1#

由于您使用的是第二代Cloud Functions for Firebase,因此您使用的是模块化API -其中调用服务器的大多数功能都在顶级函数中,而不是对象上的方法。
如果您查看模块化API中DocumentSnapshot的参考文档,您可以确认这一点,因为它没有显示updateset方法。
要更新文档,请使用文档代码示例中关于使用模块化API更新文档的语法:

import { doc, updateDoc } from "firebase/firestore";

const washingtonRef = doc(db, "cities", "DC");

// Set the "capital" field of the city 'DC'
await updateDoc(washingtonRef, {
  capital: true
});

字符串

相关问题