【发布时间】:2020-10-23 15:44:04
【问题描述】:
我已使用 @nestjs/azure-database 包将我的 NestJS 项目与 CosmosDB 数据库连接起来。我已经成功地在我的数据库中创建了第一个项目,但是每当我尝试添加另一个项目(创建函数)时,都会出现以下错误:
UnhandledPromiseRejectionWarning: Error: Entity with the specified id already exists in the system.,
RequestStartTime: 2020-10-22T11:54:32.5660620Z, RequestEndTime: 2020-10-22T11:54:32.5860730Z, Number of regions attempted:1
ResponseTime: 2020-10-22T11:54:32.5860730Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westeurope1-fd37.documents.azure.com:14162/apps/6cfd606f-f95e-4068-9026-f2f1ea7a7521/services/9ef1da94-92b6-488d-afa0-7566d7382073/partitions/4336f3e2-783d-4132-a682-84d5afed64a1/replicas/132474264435072969p/, LSN: 32, GlobalCommittedLsn: 32, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 409, SubStatusCode: 0, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#32, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Create
我的实体类是这样定义的:
import { CosmosDateTime, CosmosPartitionKey, CosmosUniqueKey } from '@nestjs/azure-database';
@CosmosPartitionKey('id')
export class TeamsSupport {
constructor(botId: string, sessionId: string, version: string, supportId: string, teamsConversationRef: string) {
this.botId = botId;
this.version = version;
this.sessionId = sessionId;
this.supportId = supportId;
this.teamsConversationRef = teamsConversationRef;
}
id?: string;
botId: string;
teamsConversationRef: string;
version: string;
supportId: string;
@CosmosUniqueKey() sessionId: string;
@CosmosDateTime() createdAt: Date;
@CosmosDateTime() updatedAt?: Date;
}
保存用户凭据,如您所见,supportId 和 teamsConversationRef 在第一个实例中未定义:
public async saveUserCredentials(sessionId: any, botId: string, version: string) {
const teamsSupport: TeamsSupport = new TeamsSupport(botId, sessionId, version, undefined, undefined);
await this.teamsSupportService.create(teamsSupport);
}
存储库类(仅创建方法):
import { Container } from '@azure/cosmos';
import { InjectModel } from '@nestjs/azure-database';
import { Injectable } from '@nestjs/common';
import { HandoverLogger } from '../logger/handoverlogger.service';
import { TeamsSupport } from './teamssupport.entity';
@Injectable()
export class TeamsSupportRepository {
constructor(@InjectModel(TeamsSupport) private readonly container: Container, private handoverLogger: HandoverLogger) {
this.handoverLogger.setContext('ContactRepository');
}
async create(item: TeamsSupport): Promise<TeamsSupport> {
item.createdAt = new Date();
const response = await this.container.items.create(item);
this.handoverLogger.verbose(`Create RUs: ${response.requestCharge}`);
return response.resource;
}
注意事项:
- sessionId 在我的测试中始终是唯一的,所以这不是问题。
- 更新我已经创建的一个项目,使用 upsert 函数没有问题
- 我已尝试删除“id?” Entity中的参数无效
我在想导致问题的原因是 CosmosDB 生成的 id,但我真的不知道。有人能帮我一下吗?提前致谢!
我能够创建的第一个项目(所有标识都用 *** 缩短):
{
"sessionId": "testId",
"createdAt": "2020-10-22T10:01:40.852Z",
"id": "368c7693-f57f-4dba-84b2-cd3c0504f088",
"_rid": "i+NVANCshdgBAAAAAAAAAA==",
"_self": "dbs/i+NVAA==/colls/i+NVANCshdg=/docs/i+NVANCshdgBAAAAAAAAAA==/",
"_etag": "\"0800f5fc-0000-0d00-0000-5f915cdd0000\"",
"_attachments": "attachments/",
"teamsConversationRef": "{\"activityId\":\"1603362010475\",\"user\":{\"id\":\"29:12zv1Zc3R***\",\"name\":\"Ros***\",\"aadObjectId\":\"33070d33-866***\",\"email\":\"jasp***\",\"userPrincipalName\":\"jaspe***\",\"tenantId\":\"8c292c0d-05c***\",\"userRole\":\"user\"},\"bot\":{\"id\":\"28:6ce0c0e***\",\"name\":\"Human Handover\"},\"conversation\":{\"isGroup\":true,\"conversationType\":\"channel\",\"tenantId\":\"8c292***\",\"id\":\"19:02bd604cd4284d4***;messageid=1603360903103\"},\"channelId\":\"msteams\",\"locale\":\"en-GB\",\"serviceUrl\":\"https://smba.trafficman***\"}",
"supportId": "29:12zv1Zc3RHpHBDKDf3***",
"updatedAt": "2020-10-22T10:20:13.681Z",
"_ts": 1603362013
}
【问题讨论】:
-
你真的在那里传递了一个 id 的值吗?这对于您插入的每个项目都是必需的。
-
我现在没有,但是 id 还是被 cosmosdb 插入的,我想这是一个差异化因素。现在,我在构造函数中添加了 id 并使用 uuid.v4() 生成了一个 id,但我仍然得到同样的错误。
标签: node.js azure-cosmosdb nestjs