我正在尝试使用 Azure 函数和 JavaScript 的 cosmosDB 客户端创建基本的 REST API。除了更新之外,我的所有操作都取得了成功。 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,
};
};
我在 Google 上搜索了这个内容,并从上到下浏览了 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();
您可以尝试使用此代码吗?
关于azure - 无法使用 JavaScript 的替换方法更新 CosmosDB 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67679949/