javascript - Cannot delete item from CosmosDB
问题描述
I am trying to use an Azure Functions httpTrigger call to delete an item from my database.
import { CosmosClient, } from '@azure/cosmos'
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
const client = new CosmosClient(process.env.cosmosDB)
const database = client.database('testDB');
const container = database.container('workers');
const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '/id')
console.log(await item.delete())
}
I even tried to hardcode the values into the code (as you can see) but I will always get 404 not found error:
Executed 'Functions.worker-delete' (Failed, Id=81ab43cf-a223-48af-89e1-a15676346ef0)
System.Private.CoreLib: Exception while executing function: Functions.worker-delete. System.Private.CoreLib: Result: Failure
Exception: Error: Entity with the specified id does not exist in the system.,
RequestStartTime: 2020-05-11T12:30:44.4010890Z, RequestEndTime: 2020-05-11T12:30:44.4010890Z, Number of regions attempted:1
ResponseTime: 2020-05-11T12:30:44.4010890Z, StoreResult: StorePhysicalAddress: xxx, LSN: 769914, GlobalCommittedLsn: 769914, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 404, SubStatusCode: 0, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#769914, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Delete
, Microsoft.Azure.Documents.Common/2.10.0
Stack: Error: Entity with the specified id does not exist in the system.,
RequestStartTime: 2020-05-11T12:30:44.4010890Z, RequestEndTime: 2020-05-11T12:30:44.4010890Z, Number of regions attempted:1
ResponseTime: 2020-05-11T12:30:44.4010890Z, StoreResult: StorePhysicalAddress: xxx, LSN: 769914, GlobalCommittedLsn: 769914, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 404, SubStatusCode: 0, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#769914, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Delete
, Microsoft.Azure.Documents.Common/2.10.0
at xxx/node_modules/@azure/cosmos/dist/index.js:6973:39
at Generator.next (<anonymous>)
at fulfilled (xxx/node_modules/tslib/tslib.js:110:62)
at process._tickCallback (internal/process/next_tick.js:68:7).
I tripple checked:
- connection string
- database name
- container name
- item-id
- partition
解决方案
通过 检索文档时container.item()
,两个参数是:
- 文件编号
- 分区键
第二个参数(分区键)需要是分区键的值。在您的示例中,您拥有分区键的路径,因此它正在检查“/id”的分区键值:
const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '/id')
这需要更改为:
const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '<partition-key-value')
推荐阅读
- bash - 为什么此 YAD 列表代码无法正常工作?
- unix - Vim:在普通模式下切换键盘布局
- angular - Spring Gateway 如何路由深度 SPA 链接
- asp.net-core - 将 blazor 组件添加到现有 Asp.net 核心项目的问题
- r - 设置种子在我的 Windows 上不起作用,因为我使用 R 从不同的示例中复制
- wpf - 为什么在我的 XAML 文件中找不到该成员?
- python - 我将如何修复此功能?
- spring - Spring:PathMatcher 拆分变量
- java - 如何在单元测试中忽略 @PreAuthorize("isAuthenticated()") 注释?
- node.js - 身体解析器行为怪异