0

I am trying to delete and update records in cosmosDB using my graphql/nodejs code and getting error - "Entity with the specified id does not exist in the system". Here is my code

deleteRecord: async (root, id) => {
  const { resource: result } = await container.item(id.id, key).delete();
  console.log(`Deleted item with id: ${id}`);
},

Somehow below code is not able to find record, even "container.item(id.id, key).read()" doesn't work.

await container.item(id.id, key)

But if I try to find record using query spec it works

await container.items.query('SELECT * from c where c.id = "'+id+'"' ).fetchNext()

FYI- I am able to fetch all records and create new item, so Connection to DB and reading/writing is not an issue.

What else can it be? Any pointer related to this will be helpful.

Thanks in advance.

user837593
  • 337
  • 1
  • 5
  • 25

1 Answers1

3

It seems you pass the wrong key to item(id,key). According to the Note of this documentation:

In both the "update" and "delete" methods, the item has to be selected from the database by calling container.item(). The two parameters passed in are the id of the item and the item's partition key. In this case, the parition key is the value of the "category" field.

So you need to pass the value of your partition key, not your partition key path. For example, if you have document like below, and your partition key is '/category', you need to use this code await container.item("xxxxxx", "movie").

{
    "id":"xxxxxx",
    "category":"movie"
}
Steve Johnson
  • 8,057
  • 1
  • 6
  • 17
  • frustratingly, this piece of documentation says nothing about partition key being neeeded: https://learn.microsoft.com/en-us/javascript/api/overview/azure/cosmos-readme?view=azure-node-latest#delete-an-data – blomster May 19 '21 at 10:51