javascript - 无法从 CosmosDB 中删除项目

标签 javascript azure-functions azure-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).

我检查了三次:
  • 连接字符串
  • 数据库名称
  • 容器名称
  • 项目 ID
  • 隔断
  • 最佳答案

    通过 container.item() 检索文档时,这两个参数是:

  • 文档 ID
  • 分区键

  • 第二个参数(分区键)需要是分区键的值。在您的示例中,您拥有分区键的路径,因此它正在检查“/id”的分区键值:
    const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '/id')
    

    这需要更改为:
    const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '<partition-key-value')
    

    关于javascript - 无法从 CosmosDB 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61729907/

    相关文章:

    javascript - 对象方法回调在作为参数传递时失去其在事件处理程序中的绑定(bind),但在硬编码时不会

    azure - 如何将 Azure 仅 blob 存储帐户与 Azure Functions 结合使用 - 尝试创建 blob 快照

    node.js - Azure Functions - NodeJS - 作为流的响应主体

    python-3.x - 如何从 Azure 函数应用发送电子邮件?

    Azure CosmosDB REST 创建文档时出错

    session - DocumentDB - 较新的 session token 是否保证读回较旧的写入?

    azure-cosmosdb - 结合 2 个字段的 IN 查询

    javascript - 通过选定的选项值设置输入值

    javascript - 使用 PHP 创建一个新的 PHP 文件

    javascript - ADAL.js - "Nonce is not same as undefined"