Reputation: 11
I am trying to use beforeRemote("create", fn). I need to change a few fields before I continue to save the data I recieve.
I've tried to manipulate the data in the ctx.args, but with no luck. The saved data does not include the changes I've made.
I am using loopback 3.23.x, along with mongodb database. Can some one tell me what to do to manipulate data. Is this not what before remote is for?
model.beforeRemote("create", async function(ctx, instance, next) {
console.log(ctx.args)
ctx.args = {
...ctx.args,
tags: [ "one", "two" ],
}
console.log(ctx.args)
return;
});
Upvotes: 1
Views: 263
Reputation: 10795
Please note that ctx.args
is containing all arguments. In your code snippet, you are setting the value for a named argument called tags
. The built-in "create" method is not accepting any tags
argument, that's why LoopBack (strong-remoting) is ignoring that extra data.
To modify the model data (property values), you need to change ctx.data
object instead.
model.beforeRemote("create", async function(ctx, instance, next) {
console.log(ctx.args)
ctx.arg.data = {
...ctx.args.data,
tags: [ "one", "two" ],
}
console.log(ctx.args)
return;
});
Upvotes: 3
Reputation: 696
After a few tests : with mysql connector, the data to create are available in ctx.req.body
.
You can modify it before data are saved into the database.
But it doesn't work with mongodb connector.
With this connectors, the created values are available into ctx.args.data
and you can modify them.
Hope it helps,
Upvotes: 0