无法从CosmosDB删除项目

我正在尝试使用Azure Functions httpTrigger调用从数据库中删除一个项目。

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

我什至尝试将值硬编码到代码中(如您所见),但我总是会得到404 not found错误:

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).

我三重检查:

  • 连接字符串
  • 数据库名称
  • 容器名称
  • 项目编号
  • 划分
评论
  • miste
    miste 回复

    When retrieving a document via container.item(), the two parameters are:

    • 文件编号
    • 分区键

    第二个参数(分区键)必须是分区键的值。在您的示例中,您有分区键的路径,因此它正在检查分区键值“ / id”:

    const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '/id')
    

    这需要更改为:

    const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '<partition-key-value')