Unable to update an item in CosmosDB using the replace method with JavaScript

3.2k Views Asked by At

I am trying to create a basic REST API using Azure functions and the cosmosDB client for JavaScript. I have been successful with all the actions except the UPDATE. The cosmosDB client uses conainter.item(id,category).replace(newObject) I am unable to get the container.item().replace method to work. When I test the function in the portal or using Postman, I get a 500 error and in the portal, I get the error: Result: Failure Exception: Error: invalid input: input is not string Stack: Error: invalid input: input is not string at trimSlashFromLeftAndRight.

Example of my basic document/item properties

{
  id:002,
  project:"Skip rope",
  category:"task",
  completed: false
}

const config = require("../sharedCode/config");
const { CosmosClient } = require("@azure/cosmos");

module.exports = async function (context, req) {
  const endpoint = config.endpoint;
  const key = config.key;
  const client = new CosmosClient({ endpoint, key });

  const database = client.database(config.databaseId);
  const container = database.container(config.containerId);

  const theId = req.params.id;

  // I am retrieving the document/item that I want to update
  const { resource: docToUpdate } = await container.item(theId).read();

  // I am pulling the id and category properties from the retrieved document/item
  // they are used as part of the replace method
  const { id, category } = docToUpdate;

  // I am updating the project property of the docToUpdate document/item
  docToUpdate.project = "Go fly a kite";

  // I am replacing the item referred to with the ID with the updated docToUpdate object
  const { resource: updatedItem } = await container
    .item(id, category)
    .replace(docToUpdate);

  const responseMessage = {
    status: 200,
    message: res.message,
    data: updatedItem,
  };

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage,
  };

};

I Googled the heck out of this and been through the Microsoft Azure CosmosDB documents from top-to-bottom, but I can't figure out how to get this to work. I can get the other CRUD operations to work based on the examples Microsoft docs provide, but not this. Any help would be greatly appreciated.

1

There are 1 best solutions below

12
On

I believe the reason you’re getting this error is because the data type of your “id” field is numeric. The data type of “id” field should be string.

UPDATE

So I tried your code and was able to run it successfully. There was one issue I noticed in your code though:

const { resource: docToUpdate } = await container.item(theId).read();

In the above line of code, you are not specifying the partition key value. If you don't specify the value, then your docToUpdate would come as undefined. In my code I used task as partition key value (I created a container with /category as the partition key).

This is the code I wrote:

const { CosmosClient } = require("@azure/cosmos");

const endpoint = 'https://account.documents.azure.com:443/';
const key = 'accountkey==';
const databaseId = 'database-name';
const containerId = 'container-name';


// const docToUpdate = {
//   'id':'e067cbae-1700-4016-bc56-eb609fa8189f',
//   'project':"Skip rope",
//   'category':"task",
//   'completed': false
// };

async function readAndUpdateDocument() {
  const client = new CosmosClient({ endpoint, key });

  const database = client.database(databaseId);
  const container = database.container(containerId);

  const theId = 'e067cbae-1700-4016-bc56-eb609fa8189f';

  const { resource: docToUpdate } = await container.item(theId, 'task').read();

  console.log(docToUpdate);
  console.log('==============================');
  const { id, category } = docToUpdate;
  docToUpdate.project = "Go fly a kite";

  console.log(docToUpdate);
  console.log('==============================');

  const { resource: updatedItem } = await container
      .item(id, category)
      .replace(docToUpdate);

  console.log(updatedItem);
  console.log('==============================');
}

readAndUpdateDocument();

Can you try by using this code?