How to include a relation to the result of a remote method in loopback?
up vote
1
down vote
favorite
I have a beforeRemote method when updating an attribute. I included a relation in context.args.include but the result returned does not include that relation. Here is an example code
Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
context.args.include = ['images'];
next();
});
What am i doing wrong? "images" attribute is not being included to the result returned.
node.js express loopback
add a comment |
up vote
1
down vote
favorite
I have a beforeRemote method when updating an attribute. I included a relation in context.args.include but the result returned does not include that relation. Here is an example code
Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
context.args.include = ['images'];
next();
});
What am i doing wrong? "images" attribute is not being included to the result returned.
node.js express loopback
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a beforeRemote method when updating an attribute. I included a relation in context.args.include but the result returned does not include that relation. Here is an example code
Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
context.args.include = ['images'];
next();
});
What am i doing wrong? "images" attribute is not being included to the result returned.
node.js express loopback
I have a beforeRemote method when updating an attribute. I included a relation in context.args.include but the result returned does not include that relation. Here is an example code
Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
context.args.include = ['images'];
next();
});
What am i doing wrong? "images" attribute is not being included to the result returned.
node.js express loopback
node.js express loopback
asked Nov 22 at 5:39
Allen Loren Batac
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I guess it doesn't work because "updateAttributes" doesn't use loopback filter to retrieve updated values, it's not a usual "find" request.
I'd use afterRemote instead of beforeRemote and add the code to retrieve datas :
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = .concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
Details here : loopback 3 remote hooks
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I guess it doesn't work because "updateAttributes" doesn't use loopback filter to retrieve updated values, it's not a usual "find" request.
I'd use afterRemote instead of beforeRemote and add the code to retrieve datas :
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = .concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
Details here : loopback 3 remote hooks
add a comment |
up vote
0
down vote
I guess it doesn't work because "updateAttributes" doesn't use loopback filter to retrieve updated values, it's not a usual "find" request.
I'd use afterRemote instead of beforeRemote and add the code to retrieve datas :
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = .concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
Details here : loopback 3 remote hooks
add a comment |
up vote
0
down vote
up vote
0
down vote
I guess it doesn't work because "updateAttributes" doesn't use loopback filter to retrieve updated values, it's not a usual "find" request.
I'd use afterRemote instead of beforeRemote and add the code to retrieve datas :
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = .concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
Details here : loopback 3 remote hooks
I guess it doesn't work because "updateAttributes" doesn't use loopback filter to retrieve updated values, it's not a usual "find" request.
I'd use afterRemote instead of beforeRemote and add the code to retrieve datas :
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = .concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
Details here : loopback 3 remote hooks
answered Nov 23 at 16:28
dun32
343
343
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424513%2fhow-to-include-a-relation-to-the-result-of-a-remote-method-in-loopback%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown