node.js - 从父模型更新相关模型

标签 node.js loopbackjs

我正在尝试使用父模型的 uRL 通过父模型创建相关模型

父级:应用程序 相关:申请人 这是下面的关系

applications --> hasMany --> applicants (外键:application ID ) applicants --> BelongsTo --> applications(外键:applicationid)

我想使用父模型的 url 对两个模型一起进行更新插入 补丁/api/应用程序

下面是applications.js文件

    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();
});

如何获取更新插入到申请人模型的结果,然后在 API 的申请响应中将其发回。

谢谢

最佳答案

这就是我要做的。肯定有更好的想法,但这可能是一个好的开始。

'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();
        }
    });
};

如果这不起作用,您应该寻找 ' before save ' Hook 。

关于node.js - 从父模型更新相关模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627894/

相关文章:

node.js - 下载带有环回的文件 4

node.js - 如何控制环回中的插入操作?

node.js - Node mysql TypeError : conn. beginTransaction 不是一个函数

javascript - 模块.exports : is not a function

loopbackjs - 我可以对环回模型使用非顺序ID吗?

node.js - Strongloop Oracle 通过脚本连接

node.js - 如何在sequelize模型中插入外键

node.js - 获取目录中的最新文件 Node.js

javascript - Nodejs - 左侧的数组

javascript - 在 beforeRemote 远程 Hook 中添加过滤器