【问题标题】:updating related models from parent model从父模型更新相关模型
【发布时间】:2018-10-04 15:42:49
【问题描述】:

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

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

applications --> hasMany --> 申请者(外键:申请 ID ) 申请人 --> BelongsTo --> 申请(外键:applicationid)

我想使用父模型的 url 对两个模型进行 upsert 补丁 /api/applications

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

如何将 upsert 的结果获取到申请者模型,然后在 api 的应用程序响应中将其发送回。

谢谢

【问题讨论】:

  • 我并没有完全理解您想要达到的目标,但请记住您的 'app.models.Applicants.upsert(...' 运行 异步 这就是为什么 response.applicants 在你的情况下总是会导致一个空数组。
  • 是否有另一种方法可以同步调用相关模型的 upsert 方法。一旦创建/修改了申请人,我想将响应发送回客户,其中包含申请详细信息和申请人详细信息示例:{“application_id”:1,“applicants”:[{“applicant_id”:1,“application_id”: 1, "name" : "abc", "country" : "IN" .......}]} 或者有没有更好的方法来处理这种要求。

标签: node.js loopbackjs


【解决方案1】:

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

'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' 挂钩。

【讨论】:

  • 感谢这工作。但是,我确实查看了 before save 钩子,我不知道如何使用 before save 钩子来执行上述操作。
  • 就像我之前说的,我不明白你的目标是什么,所以也许“保存前”对你没有用。如果有帮助,请检查答案是否已接受,谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-11-02
  • 2019-04-15
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2017-05-30
  • 2021-04-25
  • 1970-01-01
相关资源
最近更新 更多