gpt4 book ai didi

javascript - 使用 Javascript SDK 将字段添加到 CosmosDB 项目错误 : Entity with the specified id does not exist in the system

转载 作者:行者123 更新时间:2023-12-03 06:58:40 26 4
gpt4 key购买 nike

我正在尝试向 CosmosDB 项目添加新字段。我要添加的字段是一个对象。例如:

jobResult = { 
description: "exampledescription",
value: "examplevalue"
}

我尝试了以下代码:

...
const { CosmosClient } = require("@azure/cosmos");
const endpoint = "VALUE";
const key = "VALUE";
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: 'VALUE' });
const { container } = await database.containers.createIfNotExists({ id: 'VALUE' });

const operation = [ { op: "Add", path: "/result", value: jobResult } ];
const { resource: updated } = await container.item(id = '42', partitionKeyValue = '/id')
.patch(operation);
...

我收到错误:

Entity with the specified id does not exist in the system

该项目存在,因为我在使用container.item("42").read()时获取该项目

最佳答案

您为 partitionKeyValue 提供的值不正确。

您当前运行的代码正在 /Id 容器分区中查找 ID 为 42 的项目。您不需要提供路径,而是需要提供分区键的值,该值是在创建容器期间设置的。

但是,从容器创建的外观来看,似乎没有设置partitionKey路径,因此您不需要提供它:

...
const { resource: updated } = await container.item(id = '42')
.patch(operation);
...

如果您想设置partitionKey,那么您会希望容器创建如下所示:

...
const { container } = await database.containers.createIfNotExists({
id: "VALUE",
partitionKey: "/Id",
});
...

注意:您可能必须删除容器,以便使用分区键集重新创建它。

关于javascript - 使用 Javascript SDK 将字段添加到 CosmosDB 项目错误 : Entity with the specified id does not exist in the system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72668161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com