Javascript bookshelfjsfetchwithrelated选项返回空的关系对象
我有一个图像模型和一个位置模型。图像模型包含指向位置的外键。要获取结果,我使用:Javascript bookshelfjsfetchwithrelated选项返回空的关系对象,javascript,node.js,eager-loading,bookshelf.js,knex.js,Javascript,Node.js,Eager Loading,Bookshelf.js,Knex.js,我有一个图像模型和一个位置模型。图像模型包含指向位置的外键。要获取结果,我使用: fetch({withRelated: ['location']}; 我得到了以下结果: { "id": 24, "created_by": 1, "location_id": 202, "location": {} } 但我想要的是: { "id": 24, "created_by": 1, "lo
fetch({withRelated: ['location']};
我得到了以下结果:
{
"id": 24,
"created_by": 1,
"location_id": 202,
"location": {}
}
但我想要的是:
{
"id": 24,
"created_by": 1,
"location": {....}
}
我的图像模型:
objectProperties = {
tableName: 'images',
location: function () {
return this.hasOne(location, 'id');
}
};
classProperties = {};
imageModel = bookshelf.Model.extend(objectProperties, classProperties);
我的位置模型:
objectProperties = {
tableName: 'locations',
images: function () {
return this.belongsToMany(image, 'location_id');
}
};
classProperties = {};
locationModel = bookshelf.Model.extend(objectProperties, classProperties);
为什么接收到空的location对象?您在模型中的关系是错误的。您将belongtomany(仅用于m:n关系)与hasOne结合使用(不能与belongtomany一起使用)。您的问题不清楚这两张表之间有什么关系,因此我无法进一步帮助您。但问题不在于相关,而在于模型定义。希望这有帮助。您在模型中的关系是错误的。您将belongtomany(仅用于m:n关系)与hasOne结合使用(不能与belongtomany一起使用)。您的问题不清楚这两张表之间有什么关系,因此我无法进一步帮助您。但问题不在于相关,而在于模型定义。希望这有帮助。看看下面的例子
人员:身份证人员,姓名,身份证国家与相关国家:身份证国家,姓名,身份证省份与相关省份:身份证省份,姓名 确定生成个人、国家和省的模型 person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Country = require('./country');
let Person = Bookshelf.Model.extend({
tableName: 'person',
idAttribute: 'id_person',
country: function() {
return this.belongsTo(Country, 'id_country');
}
});
module.exports = Bookshelf.model('Person', Person);
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
country.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Province = require('./province');
let Country = Bookshelf.Model.extend({
tableName: 'country',
idAttribute: 'id_country',
province: function() {
return this.belongsTo(Province, 'id_province');
}
});
module.exports = Bookshelf.model('Country', Country);
province.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
let Province = Bookshelf.Model.extend({
tableName: 'province',
idAttribute: 'id_province'
});
module.exports = Bookshelf.model('Province', Province);
使用collections person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
使用controller person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
json是
{
"error":false,
"data":[{
"id_person": 1,
"name": "leonardo",
"id_country": 3,
"country":{
"id_country": 3,
"name":"venezuela",
"id_province": 2,
"province":{
"id_province": 2,
"name":"lara"
}
}
},{...}]
}
看看下面的例子
人员:身份证人员,姓名,身份证国家与相关国家:身份证国家,姓名,身份证省份与相关省份:身份证省份,姓名 确定生成个人、国家和省的模型 person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Country = require('./country');
let Person = Bookshelf.Model.extend({
tableName: 'person',
idAttribute: 'id_person',
country: function() {
return this.belongsTo(Country, 'id_country');
}
});
module.exports = Bookshelf.model('Person', Person);
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
country.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Province = require('./province');
let Country = Bookshelf.Model.extend({
tableName: 'country',
idAttribute: 'id_country',
province: function() {
return this.belongsTo(Province, 'id_province');
}
});
module.exports = Bookshelf.model('Country', Country);
province.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
let Province = Bookshelf.Model.extend({
tableName: 'province',
idAttribute: 'id_province'
});
module.exports = Bookshelf.model('Province', Province);
使用collections person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
使用controller person.js
'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
model: Person
});
module.exports = Persons;
'use strict';
const Persons = require('../collections/persons');
const Person = require('../models/person');
function getPersons(req, res, next) {
Motivos.query({})
.fetch({
withRelated: [
'country',
'country.province'
] })
.then(function(data) {
return res.status(200).json({
error: false,
data: data
});
});
}
json是
{
"error":false,
"data":[{
"id_person": 1,
"name": "leonardo",
"id_country": 3,
"country":{
"id_country": 3,
"name":"venezuela",
"id_province": 2,
"province":{
"id_province": 2,
"name":"lara"
}
}
},{...}]
}
一个图像有一个位置,但一个位置可能属于多个图像。换句话说,一对多的关系。在每个模型中使用的正确关系是什么?从文档中可以看出:当一个模型是另一个目标模型的成员时,将使用belongsTo关系。它可以在一对一关联中用作hasOne的逆。它也可以用在一对多的关联中,作为hasMany的倒数(并且是该关联的一面)!我将图像模型关系更改为
返回this.belongsTo(location)代码>和位置模型与的关系返回这个.hasMany(图像'location_id')代码>非常感谢您的帮助!一个图像有一个位置,但一个位置可能属于多个图像。换句话说,一对多的关系。在每个模型中使用的正确关系是什么?从文档中可以看出:当一个模型是另一个目标模型的成员时,将使用belongsTo关系。它可以在一对一关联中用作hasOne的逆。它也可以用在一对多的关联中,作为hasMany的倒数(并且是该关联的一面)!我将图像模型关系更改为返回this.belongsTo(location)代码>和位置模型与的关系返回这个.hasMany(图像'location_id')代码>非常感谢您的帮助!