skip to Main Content

I can’t get my trigger to update the same document that is being created. I printed out debug statements to the console and the error points to documentData.set. I’ve tried document.ref.update, documentData.ref.set, and documentData.set, but none of these work. The console shows these errors below. How do I update the same document that is being created during the create trigger?

document.ref.update: 

TypeError: Cannot read properties of undefined (reading ‘update’)

documentData.ref.set:

TypeError: Cannot read properties of undefined (reading ‘set’)

documentData.set:

TypeError: documentData.set is not a function

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

2

Answers


  1. You can only update or set a document reference, therefore in your case it should be document.data.ref.update or document.data.ref.set

    Checkout the documentation for further information.

    Login or Signup to reply.
  2. Since you’re using gen2 of Cloud Functions for Firebase, you’re using the modular API – in which most functionality that calls the server is in top-level functions rather than methods on the objects.

    If you check the reference docs of DocumentSnapshot in the modular API you can confirm this, as it shows no update or set method.

    To update the document, use the syntax from the documentation code sample on updating a document with the modular 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
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search