Every time I attempt to remove an Item using its ID, I encounter an entity not found error, even though the Item is present in the database and can be retrieved using SQL format.
I'm unable to pinpoint where the issue lies in my code because when I retrieve an item by its ID, it exists. However, attempts to delete it fail.
Here is the code for fetching:
async getAsset(assetId: string): Promise<Asset> {
const assetsDb = await getDbClient(cosmoDBContainers.assets);
const response = await assetsDb.items
.query<Asset>({
query: `SELECT * FROM ${cosmoDBContainers.assets} A
WHERE A.id = @id`,
parameters: [{ name: "@id", value: assetId }],
}).fetchAll();
return response.resources.map(x => new Asset(
x.id,
x.orgId,
x.assetType,
x.title,
x.createdAt,
x.lastUpdate,
x.fileStorageId,
))[0];
} // item successfully retrieved
This is the deletion code (note that I invoke the getAsset function):
async removeAsset(assetId: string): Promise<void> {
const assetsDb = await getDbClient(cosmoDBContainers.assets);
const asset = await this.getAsset(assetId); // verifying IDs match, which they do
const item = assetsDb.item(asset.Id); // this line executes correctly
await item.delete(); // error occurs here
}
The partition key in CosmosDB is "/id". I have attempted adding it when calling assetsDb.item(asset.Id, "/id")
, but the same "not found" error persists.
Here is the full error message:
Error: Entity with the specified id does not exist in the system.
I've tested several solutions from other Stack Overflow answers, but none seem to resolve the issue. It appears to be a problem on my end, and I am currently at a loss. Any insights would be greatly appreciated.
I am working with TypeScript and "@azure/cosmos": "^3.11.0"
Thank you.