4

I have a Stitch function that does a couple of things and I'm getting an error at this line:

try {
  // voteAsObjectId is the Id of the photo to vote converted to BSON.ObjectId.
  await db
    .collection('photos')
    .updateOne({ "_id": voteAsObjectId }, { "$inc": { votes: 1 }});
} catch (err) {
  throw new Error("An error occurred while updating photo's votes:" + err);
}

The error is the following:

StitchError: update not permitted

It seems like a permissions related error, but I have some other functions in my app and they successfully run every .find .insert and .aggregate queries to this collection, only the .updateOne and .updateMany operations are giving me problems.

My collection's permissions look like this:

enter image description here

This is my schema:

enter image description here

And here's a sample document:

enter image description here

DoHn
  • 625
  • 7
  • 15

1 Answers1

2

I would recommend checking the following:

  1. Check the Schema tab and seeing if there's anything about your documents that doesn't fit the configured schema. The schema has a default configuration that may not match the actual schema of your documents. You may need to modify this schema to get the update working.

  2. Click the edit button (the pencil) on the 'default' role, and check the "Apply When" of the role. It's possible that the document you're trying to update does not match any role, and that the role needs updating.

If that doesn't work, check the application logs by clicking the "Logs" button on the sidebar: Logs tab

There you can see the "update not permitted" error but with more details about why the update failed.

adamchel
  • 46
  • 1
  • 3
  • The error is taken from the logs, there is no other info (apart from the function that caused it and its arguments). I checked the schema multiple times and it seems right to me, but I'll add the screenshot if you think that's the problem. The Apply When is set to {}, I don't know if that's right or not. – DoHn Apr 09 '19 at 17:42
  • Apply When set to {} means that role will apply for every document so that is probably not the problem. Can you send a screenshot of the schema and a sample document from your collection? – adamchel Apr 09 '19 at 19:12
  • Hmm, I wonder if there's a bug where it's trying to convert the number to an int32 for some reason when doing the increment. What happens if you omit "votes" from the schema entirely? (We implicitly allow all fields to be any time, so if it's a problem with the "votes" field this should make the error go away.) – adamchel Apr 09 '19 at 19:48
  • I tried to update the "title" field using $set and it still gives the same error. – DoHn Apr 09 '19 at 20:01
  • Is it possible that the document you're trying to update has fields that don't match the schema because they were inserted outside of Stich? In other words, can you run "await db.collection('photos') .findOne({ "_id": voteAsObjectId })" and print the document to make sure it matches the schema? – adamchel Apr 10 '19 at 16:06
  • All documents are added through a Stitch function. I did what you said and the findOne returns a document that looks right to me. Also, I don't know if it helps, but I noticed strange behaviour in the code that followed as well. Basically, after this piece of code, the function updates the users collection by pushing an item to an array, and, no matter which way I do it, instead of pushing an item to the array it entirely removes the array field. I'm thinking update operations on Stitch are not working as intended. – DoHn Apr 10 '19 at 16:30
  • Could you share more of the code from your function? It may be possible that the "update not permitted" error is happening due to an error with a different update against a different collection. The line numbers in the errors may not be entirely reliable. You could try adding "console.log" statements in your function to determine where exactly the function stops executing. – adamchel Apr 10 '19 at 22:37
  • The code is inside a try/catch block, I'm manually throwing the error inside the catch statement so I can be sure that it's getting thrown by that promise. I've updated my question to show how I'm handling the error. – DoHn Apr 10 '19 at 23:24