更新总是插入最后一个数组值
update always inserting last array value
我是 node 的新手,搜索了很多找到解决方案,但不知道如何使用我的条件,如果有人可以帮助我解决这个问题
Permissiontb.assembleAndInsert = async (ctx, cb) => {
for (let i = 0; i < ctx.req.body.view.length; i++) {
console.log('outside i '+i)
await Process(ctx,i);
}
};
function Process(ctx,i) {
let data={company_id:ctx.args.options.accessToken.userId,
userid:ctx.req.body.userid,
perpage:ctx.req.body.perpage[i].id,
view:ctx.req.body.view[i],
edit:ctx.req.body.edit[i],
update:ctx.req.body.update[i],
delete:ctx.req.body.delete[i]}
console.log('inside '+i)
return new Promise((resolve, reject) => {
Permissiontb.find({where:{and: [{userid:ctx.req.body.userid}, {perpage:ctx.req.body.perpage[i].id}] }},function(err,result){
if(err){
var err = new Error('Some thing went wrong');
err.statusCode = 444;
console.log('err'+err)
resolve(err)
}else{ console.log(result.length)
if(result.length>0){
console.log(ctx.req.body.userid)
console.log('page id '+ctx.req.body.perpage[i].id)
const pgid=ctx.req.body.perpage[i].id
const vd=ctx.req.body.view[i]
const ed=ctx.req.body.edit[i]
const up=ctx.req.body.update[i]
const del=ctx.req.body.delete[i]
console.log(ctx.req.body.view[i])
console.log(ctx.req.body.edit[i])
console.log(ctx.req.body.update[i])
console.log(ctx.req.body.delete[i])
console.log('findinside update '+i)
resolve(Permissiontb.update({where:{and: [{userid:ctx.req.body.userid}, {perpage:pgid}] }},{view:vd,edit:ed,update:up,delete:del}))
}else{
resolve(Permissiontb.create([data]))
}
}
})
})
}
现在的问题是 if(result.lenght>0) 条件下的控制台日志,例如:- console.log(ctx.req.body.view[i]) 获取正确的值,但是当更新方法总是插入最后一个数组值 谁能告诉我我做错了什么
最后我发现灵魂可能会帮助其他像我一样面临问题的人
var async = require('async');
module.exports = function(Permissiontb) {
Permissiontb.assembleAndInsert = async (ctx, cb) => {
for (let i = 0; i < ctx.req.body.view.length; i++) {
await Process(ctx,i);
}
};
Permissiontb.remoteMethod('assembleAndInsert', {
http: {
path: '/assembleAndInsert',
verb: 'post',
},
accepts: [{ arg: 'data', type: 'object', http: { source: 'context' } },
{"arg": "options", "type": "object", "http": "optionsFromRequest"}],
returns: {
arg: 'data',
type: 'object',
},
});
function Process(ctx,i) {
let data={company_id:ctx.args.options.accessToken.userId,
userid:ctx.req.body.userid,
perpage:ctx.req.body.perpage[i].id,
view:ctx.req.body.view[i],
edit:ctx.req.body.edit[i],
update:ctx.req.body.update[i],
delete:ctx.req.body.delete[i]}
return new Promise((resolve, reject) => {
resolve(Permissiontb.upsertWithWhere({userid:ctx.req.body.userid,perpage:ctx.req.body.perpage[i].id},data))
})
}
问题是我正在使用 Permissiontb.upsertWithWhere(//我在这里使用 where->{userid:ctx.req.body.userid,perpage:ctx.req.body.perpage[i].id},data))
我是 node 的新手,搜索了很多找到解决方案,但不知道如何使用我的条件,如果有人可以帮助我解决这个问题
Permissiontb.assembleAndInsert = async (ctx, cb) => {
for (let i = 0; i < ctx.req.body.view.length; i++) {
console.log('outside i '+i)
await Process(ctx,i);
}
};
function Process(ctx,i) {
let data={company_id:ctx.args.options.accessToken.userId,
userid:ctx.req.body.userid,
perpage:ctx.req.body.perpage[i].id,
view:ctx.req.body.view[i],
edit:ctx.req.body.edit[i],
update:ctx.req.body.update[i],
delete:ctx.req.body.delete[i]}
console.log('inside '+i)
return new Promise((resolve, reject) => {
Permissiontb.find({where:{and: [{userid:ctx.req.body.userid}, {perpage:ctx.req.body.perpage[i].id}] }},function(err,result){
if(err){
var err = new Error('Some thing went wrong');
err.statusCode = 444;
console.log('err'+err)
resolve(err)
}else{ console.log(result.length)
if(result.length>0){
console.log(ctx.req.body.userid)
console.log('page id '+ctx.req.body.perpage[i].id)
const pgid=ctx.req.body.perpage[i].id
const vd=ctx.req.body.view[i]
const ed=ctx.req.body.edit[i]
const up=ctx.req.body.update[i]
const del=ctx.req.body.delete[i]
console.log(ctx.req.body.view[i])
console.log(ctx.req.body.edit[i])
console.log(ctx.req.body.update[i])
console.log(ctx.req.body.delete[i])
console.log('findinside update '+i)
resolve(Permissiontb.update({where:{and: [{userid:ctx.req.body.userid}, {perpage:pgid}] }},{view:vd,edit:ed,update:up,delete:del}))
}else{
resolve(Permissiontb.create([data]))
}
}
})
})
}
现在的问题是 if(result.lenght>0) 条件下的控制台日志,例如:- console.log(ctx.req.body.view[i]) 获取正确的值,但是当更新方法总是插入最后一个数组值 谁能告诉我我做错了什么
最后我发现灵魂可能会帮助其他像我一样面临问题的人
var async = require('async');
module.exports = function(Permissiontb) {
Permissiontb.assembleAndInsert = async (ctx, cb) => {
for (let i = 0; i < ctx.req.body.view.length; i++) {
await Process(ctx,i);
}
};
Permissiontb.remoteMethod('assembleAndInsert', {
http: {
path: '/assembleAndInsert',
verb: 'post',
},
accepts: [{ arg: 'data', type: 'object', http: { source: 'context' } },
{"arg": "options", "type": "object", "http": "optionsFromRequest"}],
returns: {
arg: 'data',
type: 'object',
},
});
function Process(ctx,i) {
let data={company_id:ctx.args.options.accessToken.userId,
userid:ctx.req.body.userid,
perpage:ctx.req.body.perpage[i].id,
view:ctx.req.body.view[i],
edit:ctx.req.body.edit[i],
update:ctx.req.body.update[i],
delete:ctx.req.body.delete[i]}
return new Promise((resolve, reject) => {
resolve(Permissiontb.upsertWithWhere({userid:ctx.req.body.userid,perpage:ctx.req.body.perpage[i].id},data))
})
}
问题是我正在使用 Permissiontb.upsertWithWhere(//我在这里使用 where->{userid:ctx.req.body.userid,perpage:ctx.req.body.perpage[i].id},data))