如何使用MongoDB数据源增加环回4中的计数器变量?
我正在尝试将我的Nodejs Express应用程序转换为Loopback 4,但我不知道如何增加计数器。在我的Angular 9应用程序中,当用户单击图标时,计数器将递增。这在Express中非常有效 在Express中如何使用MongoDB数据源增加环回4中的计数器变量?,mongodb,loopback4,Mongodb,Loopback4,我正在尝试将我的Nodejs Express应用程序转换为Loopback 4,但我不知道如何增加计数器。在我的Angular 9应用程序中,当用户单击图标时,计数器将递增。这在Express中非常有效 在Express中 const updateIconCount=async函数(数据集,集合='icons'){ let query={u id:newobjectid(dataset.id),userId:dataset.userId}; return wait mongoController
const updateIconCount=async函数(数据集,集合='icons'){
let query={u id:newobjectid(dataset.id),userId:dataset.userId};
return wait mongoController.update(
收集
查询
{$inc:{counter:1}},
函数(err、res){
日志访问(res,'debug',true,'function update updateconlink');
如果(错误){
返回false;
}否则{
返回true;
}
}
);
};
我试图先获取计数器的值,然后再递增,但每次我保存VS代码时,都会以一种不同寻常的方式重新格式化代码。在这段代码中,我注释掉了导致这种重新格式化的代码行。我可以设置计数器值,例如100
在环回4中
@patch('/icons/count/{id}){
答复:{
'204': {
描述:“图标补丁成功”,
},
},
})
异步递增CountById(
@param.path.string('id')id:string,
@请求主体({
内容:{
“应用程序/json”:{
schema:getModelSchemaRef(图标,{partial:true}),
},
},
})
图标:图标,
):承诺{
//让targetion=this.findById(id).then(icon=>{returnicon});
icons.counter=100;
控制台.log(图标.计数器);
等待此.iconsRepository.updateById(id,图标);
}
如何在环回4中实现{$inc:{counter:1}}
添加到援助解决方案中
My mongo.datasource.ts
从'@loopback/core'导入{inject,lifeCycleObserver,lifeCycleObserver};
从'@loopback/repository'导入{juggler};
常量配置={
名称:“mongo”,
连接器:“mongodb”,
url:“”,
主持人:“192.168.253.53”,
港口:32813,
用户:“”,
密码:“”,
数据库:“firstgame”,
useNewUrlParser:true,
allowExtendedOperators:true,
};
//观察应用程序的生命周期以在以下情况下断开数据源
//应用程序已停止。这允许关闭应用程序
//优雅地。'stop()'方法是从'juggler.DataSource'继承的。
//了解更多信息,请访问https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('数据源')
导出类MongoDataSource扩展了juggler.DataSource
实现LifeCycleObserver{
静态数据源名称='mongo';
静态只读defaultConfig=config;
建造师(
@inject('datasources.config.mongo',{optional:true})
dsConfig:object=config,
) {
超级(dsConfig);
}
}
修改的端点
@patch('/icons/count/{id}', {
responses: {
'204': {
description: 'Icons PATCH success',
},
},
})
async incrementCountById(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Icons, {partial: true}),
},
},
})
icons: Icons,
): Promise<void> {
console.log(id);
// @ts-ignore
await this.iconsRepository.updateById(id, {$inc: {counter: 1}});//this line fails
// icons.counter = 101; //these lines will set the icon counter to 101 so I know it is connecting to the mongodb
// await this.iconsRepository.updateById(id, icons);
}
@patch('/icons/count/{id}){
答复:{
'204': {
描述:“图标补丁成功”,
},
},
})
异步递增CountById(
@param.path.string('id')id:string,
@请求主体({
内容:{
“应用程序/json”:{
schema:getModelSchemaRef(图标,{partial:true}),
},
},
})
图标:图标,
):承诺{
console.log(id);
//@ts忽略
等待this.iconsRepository.updateById(id,{$inc:{counter:1}});//此行失败
//icons.counter=101;//这些行将图标计数器设置为101,因此我知道它正在连接mongodb
//等待此.iconsRepository.updateById(id,图标);
}
您可以使用mongo
基本上,您只需在MongoDB数据源定义()中设置allowExtendedOperators=true
。之后,您可以直接使用这些运算符
用法示例:
//按3递增icon.counter
等待this.iconsRepository.updateById(id,{$inc:{counter:3}}作为部分);
目前,lb4类型中缺少这些运算符,因此必须欺骗typescript才能接受它们。这很难看,但这是我现在能找到的唯一解决办法
您可以查看这些操作员的情况。谢谢您提供的信息。我阅读了所有关于这个的文档,令人沮丧的是,它现在返回了补丁/icons/count/5f14e97ad00d3506bf85a85:500 MongoError:the dollar($)“$inc”中带前缀的字段“$inc”对存储无效。我在
mongo.datasource.ts
中设置了allowExtenderOperators:true
,我为此构建了一个示例,它可以正常工作。如果你能提供更多的信息,我可以帮你解决这个问题。非常感谢。我在上面的问题中添加了我的mongo.datasource.ts
,只是为了再次检查我是否在正确的位置设置了allowExtenderOperators:true
,以及我修改后的endpint。我的环回连接器mongodb包
是5.3.0
。对不起,是我的错:)。我打错了,它是allowExtendedOperators而不是AllowExtendeOperators。我将编辑答案,以确保没有其他人对此感到困惑。哈哈,我有点疯了。谢谢你的帮助,我应该仔细检查一下。它按预期运行,世界再次变得有意义了!:)