Javascript 如何通过updateOrCreate更新环回中的属性?
我想用远程方法更新属性,但它不能正常工作, 我想将年龄从应用程序发布到后端,并在后端进行更新Javascript 如何通过updateOrCreate更新环回中的属性?,javascript,node.js,loopbackjs,loopback,Javascript,Node.js,Loopbackjs,Loopback,我想用远程方法更新属性,但它不能正常工作, 我想将年龄从应用程序发布到后端,并在后端进行更新 Person.testupdate = function ( id, age, cb) { Person.upsertWithWhere({ where: { id: id }, age: age, }, function (err, Person) { cb(null, Person);
Person.testupdate = function ( id, age, cb) {
Person.upsertWithWhere({
where: {
id: id
},
age: age,
},
function (err, Person) {
cb(null, Person);
});
}
Person.remoteMethod('testupdate', {
accepts: [{
arg: 'id',
type: 'string'
}
],
returns: {
arg: 'result',
type: 'string'
},
http: {
path: '/updateage',
verb: 'get'
}
});
};
你能试试这段代码吗?首先你需要通过id参数找到记录,更新年龄字段,然后保存它 查看remoteMethod定义中的accepts属性,您还需要添加age参数
Person.testupdate = function (id, age, cb){
Person.findById(id, function (err, person){
person.age = age;
return person.save(function (err, personSaved){
cb(null, personSaved);
})
})
}
Person.remoteMethod('testupdate', {
accepts: [{
arg: 'id',
type: 'string',
required: true
},
{
arg: 'age',
type: 'number',
required: true
}
],
returns: {
arg: 'result',
type: 'object'
},
http: {
path: '/updateage',
verb: 'POST'
}
})
在以下位置签出环回API文档: PersistedModel.upsertWithWhere([where],数据,回调) 根据搜索条件更新或插入模型实例。如果有 检索到单个实例,请更新检索到的模型。创建一个新的 如果找不到模型实例,则创建模型。如果出现多个错误,则返回一个错误 实例被找到 这里的
数据
应该是一个对象
Person.testupdate = function ( id, age, cb) {
Person.upsertWithWhere({
where: {
id: id // or just where : { id } in shorthand
},
// age: age,
{age: age}, // or just {age} in shorthand
},
function (err, Person) {
cb(null, Person);
});
}
Person.remoteMethod('testupdate', {
accepts: [{
arg: 'id',
type: 'string'
}
],
returns: {
arg: 'result',
type: 'string'
},
http: {
path: '/updateage',
verb: 'get'
}
});
};
此外,您正在返回一个模型实例,但返回参数是string。你可能想改变这一点
看看它是否有效。我觉得休息没问题