我正在尝试使用 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 操作正常工作,但不是这个。任何帮助将不胜感激。