Loopback无法在模型JS文件中的相关模型上创建和操作
问题描述:
无法通过model.json文件在model.js文件中创建关系,它工作正常。我想更新出价表中任何更新的预订表字段,但我的关系在model.js文件中不起作用。Loopback无法在模型JS文件中的相关模型上创建和操作
Bidding.belongsTo(Booking,{foreignKey:'bookingId'}); ^ 的ReferenceError:预订没有定义
{
"error": {
"statusCode": 500,
"name": "TypeError",
"message": "Bidding.booking is not a function",
"stack": "TypeError: Bidding.booking is not a function\n.."
}
}
'use strict';
//var loopback = require('loopback');
//var boot = require('loopback-boot');
//var app = module.exports = loopback();
module.exports = function(Bidding) {
\t // var app = require('../../server/server');
// var Booking = app.models.Booking;
//Bidding.belongsTo(Myuser, {foreignKey: 'driver_id'});
Bidding.belongsTo(Booking, {foreignKey: 'bookingId'});
Bidding.observe('before save', function beforeSave(ctx, next) {
if (ctx.instance) {
//on create
ctx.instance.created = new Date();
ctx.instance.modified = new Date();
} else {
// on edit
// ctx.instance.lastUpdated = new Date();
console.log('updatesdd');
//Bidding.Booking.upsertWithWhere({id: ctx.instance.id},{ 'username': username}, function(err, results) {});
Bidding.booking(function(err, booking) {
console.log(ctx.booking);
});
}
next();
});
Bidding.observe('loaded', function beforeaccess(ctx, next) {
console.log(ctx.data);
next();
});
};
答
通过以下方式,您可以在您涉及的表执行的操作。我浏览内置的user.js文件,它们与AccessToken表有关,并为我的问题找到以下解决方案。
查询其加载您相关表格 -
这里的“预订”模式具有属于关系在bidding.json VAR预订= ctx.Model.relations.booking.modelTo定义;
更新查询呼叫通过StrongLoop API浏览器:
http://0.0.0.0/api/Biddings/update?where=%7B%22id%22%3A%2211%22%2C%22bookingId%22%3A6%7D
其中:{ “ID”: “11”, “bookingId”:6} 数据:{ “bid_status”: “接受”}
Bidding.observe('before save', function beforeSave(ctx, next) {
if (ctx.instance) {
//on create
ctx.instance.created = new Date();
ctx.instance.modified = new Date();
} else {
//on update
if(ctx.data.bid_status == "Accept");
{
if (ctx.where && ctx.where.bookingId && ctx.where.id) {
var bookingId = ctx.where.bookingId;
var booking = ctx.Model.relations.booking.modelTo;
booking.upsertWithWhere({id: bookingId},{ 'booking_status': 'Confirmed'}, function(err, results) {});
}
}
}
next();
});