Loopbackjs 如何在环回js中动态添加字段?
我的前端查询: {%22where%22:{%22emp_代码%22:%22EMPT01%22} 原始where查询:{“where”:{“emp_代码”:“EMPT01”} 我试图在上述传入请求中动态添加“code”字段Loopbackjs 如何在环回js中动态添加字段?,loopbackjs,strongloop,Loopbackjs,Strongloop,我的前端查询: {%22where%22:{%22emp_代码%22:%22EMPT01%22} 原始where查询:{“where”:{“emp_代码”:“EMPT01”} 我试图在上述传入请求中动态添加“code”字段 Employee.observe('access', function (ctx, next) { // first way, it is not adding "orgId" field. ctx.query.where = {
Employee.observe('access', function (ctx, next) {
// first way, it is not adding "orgId" field.
ctx.query.where = {
orgId: ctx.options.data.orgId
};
// second way, it is not adding "orgId" field.
const query = {};
query['orgId'] = ctx.options.data.orgId;
ctx.query.where = query;
next();
});
请有人引导我,哪里不对
环回版本:3
谢谢,我用这种方法解决了这个问题
Employee.observe('access', function (ctx, next) {
// instead single , check both condition
if(ctx.query.where !=undefined){
// if the request does not contain **where** then add it & filter it
ctx.query.where.orgId = ctx.options.data.orgId;
}else{
ctx.query.where = {
orgId: ctx.options.data.orgId
};
}
next();
});
get请求中的
ctx.options.data
来自哪里?我已经在没有它的情况下测试了你的代码,它正在工作。另外,您没有添加到查询中,而是将其替换。ctx.options.data:它来自中间件。请尝试发布连接器调试字符串。