你可以在这个例子中做类似的事情
@post('/characters', {
responses: {
'200': {
description: 'Character model instance',
content: {'application/json': {schema: {'x-ts-type': Character}}},
},
},
})
async create(@requestBody() character: Character): Promise<Character> {
//add following lines
let characterId = 1;
while(await this.characterRepository.exists(characterId)){
characterId ++;
}
character.id = characterId;
//add above lines
return await this.characterRepository.create(character);
}
您可能已经注意到了自动递增id特性。当您多次调用post API时(将id留空),id每次增加1。内存数据库支持此功能。但是我们在这个项目中使用MongoDB。如果我们想拥有这个功能,我们需要通过编程来实现。
更多信息请点击以下链接
https://strongloop.com/strongblog/building-online-game-with-loopback-4-pt1/
请参阅API资源管理器标题上方的部分
或查找“自动增量id”,您将被带到该段落
希望这能帮上忙,如果还有其他问题,请写信给我。
谢谢