从父模型更新相关模型
updating related models from parent model
我正在尝试使用父模型uRL通过父模型创建相关模型
家长:申请
相关:申请人
这是下面的关系
applications --> hasMany --> applicants(外键:application
ID )
applicants --> BelongsTo --> applications(外键:applicationid)
我想使用父模型的 url 对两个模型一起进行更新插入
补丁 /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();
});
我如何获取更新插入到申请人模型的结果,然后在 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' 钩子。
我正在尝试使用父模型uRL通过父模型创建相关模型
家长:申请 相关:申请人 这是下面的关系
applications --> hasMany --> applicants(外键:application ID ) applicants --> BelongsTo --> applications(外键:applicationid)
我想使用父模型的 url 对两个模型一起进行更新插入 补丁 /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();
});
我如何获取更新插入到申请人模型的结果,然后在 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' 钩子。