Search code examples
node.jsloopbackjs

updating related models from parent model


I am trying to create a related model via the parent model using the uRL of the parent model

Parent : Applications Related : Applicants This is the below relationship

applications --> hasMany -- > applicants ( foreignkey : application id ) applicants --> BelongsTo --> applications (foreignkey : applicationid )

I want to do an upsert on both the models togther using the url of the parent model PATCH /api/applications

Below is the applications.js file

    module.exports = function(Application) 
    {
    Application.afterRemote('upsert', function(ctx, instance, next){
      var response ={};
      response.id = ctx.result.id;

    var applicants = ctx.req.body.applicants || undefined;
    Application.getApp(function (err, app) { 
        var resp = { "applicants" :[]};
        if (applicants){
        for (var i=0; i<applicants.length ; i++)
        {
            applicants[i].application_id = ctx.result.id;
            app.models.Applicants.upsert(applicants[i], function (err, result) 
            {
                if (err) throw err;
                if (!result) {
                    var err = new Error("Insert into Applicant failed");
                    err.statusCode = 500;
                    next(err);
                }
                 // This prints the result in the console.
                console.log('***** In APP' + JSON.stringify(result));
                resp.applicants.push(result);
            });
            console.log(JSON.stringify(applicants));
        }
        // This still results in a empty array
        response.applicants = resp.applicants;
        ctx.result = response;
    }
    });
    next();
});

How can I fetch the result of the upsert to the applicants model and then send it back in the application response for the api.

Thanks


Solution

  • This is what I would do. There is certainly a better way thought but it might be a good start.

    'use strict';
    
    var app = require('../../server/server');
    var models = app.models;
    var Applicants;
    app.on('started', function () {
        Applicants = models.Applicants;
    });
    
    module.exports = function (Application)
    {
        Application.afterRemote('upsert', function (ctx, instance, next) {
            var response = {};
            response.id = ctx.result.id;
            var applicants = ctx.req.body.applicants || undefined;
    
            if (applicants) {
                var resp = {"applicants": []};
                var count = 0;
                for (var i = 0, applicantsLength = applicants.length; i < applicantsLength; i++) {
                    applicants[i].application_id = ctx.result.id;
                    Applicants.upsert(applicants[i], function (err, result) {
                        if (err)
                            return next(err);
                        if (!result) {
                            var err = new Error("Insert into Applicant failed");
                            err.statusCode = 500;
                            return next(err);
                        }
                        resp.applicants.push(result);
                        count++;
                        if (count === applicantsLength) {
                            response.applicants = resp.applicants;
                            ctx.result = response;
                            next();
                        }
                    });
                }
            } else {
                next();
            }
        });
    };
    

    If this is not working, you should look for the 'before save' hook.