Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Values to the next() in remote hooks. #4320

Closed
boakye-agyeman opened this issue Apr 2, 2020 · 2 comments
Closed

Passing Values to the next() in remote hooks. #4320

boakye-agyeman opened this issue Apr 2, 2020 · 2 comments

Comments

@boakye-agyeman
Copy link

MyModel.afterRemote('create', function(ctx, unused,next){
var NewModel = MyModel.models.User
//Now I will pass the context variables to a variable.
// Example
var body = ctx.req.body;
username = body.Username;
email = body.email;

// I want to create an object and persist into a different model instance. I called that model at the // // top as NewModel referencing the User model.
// The creation was done by manipulating the current ctx variables.
NewModel.create({Variables to be persisted goes here}, function(err,instance){
})
})
......

Now what I want is to pass the instance of the NewModel Creation to the next in the beforeRemote hook. How do I go about it? Ill be glad to provide further details

@boakye-agyeman boakye-agyeman changed the title Passing Values to the next() i remote hooks. Passing Values to the next() in remote hooks. Apr 2, 2020
@youknowme09
Copy link

If I'm not misunderstand ... try this

model.exports = function(MyModel) {
    let newModelInstance = null;
    MyModel.afterRemote('create', function(ctx, unused, next) {
        var NewModel = MyModel.models.User
        var body = ctx.req.body;
        username = body.Username;
        email = body.email;
        //..
        NewModel.create({
            //Variables to be persisted goes here
        }, function(err, instance) {
            newModelInstance = instance;//for next in the beforeRemote reference
        })
    })

    MyModel.beforeRemote('create', function(ctx, unused, next) {
        console.log(newModelInstance);//should be last assign instance in memory...
    })
}

@deepakrkris
Copy link

@boakye-agyeman next() only takes an error argument, we cannot pass any data values into it. please take a look at https://loopback.io/doc/en/lb3/Remote-hooks.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants