Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string. /*what is empty or not a string*/

Viewed 63

this is the error that I get. I checked multiple times that the paths that I indicate are actually pointing at something in the database. I'm kinda going crazy about why this is not working, so help will be appreciated. (the same error is given by both functions, two times every invocation of the function) this is my code:

exports.onCreatePost = functions.firestore
  .document('/time/{userid}/date/{postId}')
  .onCreate (async (snapshot, context) => {
      const postCreated = snapshot.data();
      const userID = context.params.userid;
      const postID = context.params.postId;

      //get all the followers who made the post
      const userFollowerRef = admin.firestore().collection('time').doc(userID).collection('followers');
      const querySnap = await userFollowerRef.get();

      //add the post in each follower timeline 
      querySnap.forEach(doc => {
          const followerid = doc.id;
          admin.firestore().collection('time').doc(followerid).collection('timelinePosts').doc(postID).set(postCreated);
      })
  });

//when a post is updated
exports.onUpdatePost = functions.firestore
  .document('/time/{userid}/date/{postid}')
  .onUpdate(async (change, context) => {
      const postUpdated = change.after.data();
      const userID = context.params.userid;
      const postID = context.params.postId;
      //get all the followers who made the post
      const userFollowerRef = admin.firestore().collection('time').doc(userID).collection('followers');
      const querySnap = await userFollowerRef.get();

      //Update the post in each follower timeline
      querySnap.forEach(doc => {
          const follower = doc.id;
          admin.firestore().collection('time').doc(follower).collection('timelinePosts').doc(postID)
              .get().then(doc => {
                  if (doc.exists) {
                      doc.ref.update(postUpdated);
                  }
              });
      });
  });

I personally don't know how to log each variable and did not find how to do it online. I'll keep searching but in the mindtime I can share my extensive logs that from my interpretation are not very useful but maybe is just because I'm inexperienced. this is the error log enter image description here

1 Answers

In function exports.onUpdatePost ...you're likely trying to access documentPath null (or something alike that). Add logging, this permits to log custom debug information into the log which you've screenshotted. When logging every step of the procedure, it's a whole lot easier to determine what is happening and why - or why not, when skipping something. Alike this you should be able to solve the issue on your own. My functions logging actually utilizes emojis, because UTF-8 is being supported: ✅❌ (visual indicators make the log more readable).

The cause seems to be one of these instructions:

admin.firestore().collection('time') // it is being assumed that it exists
    .doc(userID)                     // argument or return value may be null
    .collection('followers')         // return value may be null

Or:

admin.firestore().collection('time') // it is being assumed that it exists
   .doc(follower)                    // argument or return value may be null
   .collection('timelinePosts')      // return value may be null
   .doc(postID)                      // argument or return value may be null

eg. one first has to check if follower != null or empty and if the desired document even exists. The same goes for userID and .doc(userID) (the seemingly "own" timeline).

if (follower != null && follower.length > 0) {
    admin.firestore().collection('time').doc(follower).get().then(timeline => {
        functions.logger.log('timeline: ' follower + ', ' + timeline.exists);
        if (timeline.exists) {
          
        } else {

        }
    });
}

documentPath == null comes from .doc() + userID, followerid, follower or postID.

Related