admin管理员组文章数量:1323734
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
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
-
1
The 2nd parameter (partition key) in your call to
container.item()
should be the value of your partition key, not the path to the partition key. – David Makogon Commented May 11, 2020 at 12:52 - 1 holy cow, this is working - if you post it as an anwser, i will mark it "correct answer". Thanks a lot. – Woozar Commented May 11, 2020 at 12:55
1 Answer
Reset to default 10When retrieving a document via container.item()
, the two parameters are:
- document id
- partition key
The 2nd parameter (partition key) needs to be the value of the partition key. In your example, you had the path to the partition key instead, so it's checking for a partition key value of "/id":
const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '/id')
This needs to be changed to:
const item = container.item('28a31558-ff8c-40c3-a7e8-1e8904c5ff72', '<partition-key-value')
本文标签: javascriptCannot delete item from CosmosDBStack Overflow
版权声明:本文标题:javascript - Cannot delete item from CosmosDB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118769a2421594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论