无法使用 JavaScript 的替换方法更新 CosmosDB 中的项目

问题描述

我正在尝试使用 Azure 函数和用于 JavaScript 的 cosmosDB 客户端创建一个基本的 REST API。除了 UPDATE 之外,我的所有操作都成功了。 cosmosDB 客户端使用 conainter.item(id,category).replace(newObject) 我无法让 container.item().replace 方法工作。当我在门户中或使用 Postman 测试该功能时,出现 500 错误,在门户中出现错误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,};

};

我在谷歌上搜索了这个,并从上到下浏览了 Microsoft Azure CosmosDB 文档,但我不知道如何让它工作。我可以根据 Microsoft 文档提供的示例使其他 CRUD 操作工作,但不是这个。任何帮助将不胜感激。

解决方法

我相信您收到此错误的原因是“id”字段的数据类型是数字。 “id”字段的数据类型应为字符串。

更新

所以我尝试了你的代码并且能够成功运行它。不过,我在您的代码中注意到了一个问题:

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

在上面的代码行中,您没有指定分区键值。如果您不指定该值,那么您的 docToUpdate 将变为 undefined。在我的代码中,我使用 task 作为分区键值(我创建了一个以 /category 作为分区键的容器)。

这是我写的代码:

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();

您可以尝试使用此代码吗?