Bookshelf js fetch withRelated选项返回空关系对象
Bookshelf js fetch withRelated option returns empty relation object
我有一个图像模型和一个位置模型。图像模型包含location的外键。为了获取结果,我使用:
fetch({withRelated: ['location']};
和I收到以下结果:
{
"id": 24,
"created_by": 1,
"location_id": 202,
"location": {}
}
但是我想要这样的:
{
"id": 24,
"created_by": 1,
"location": {....}
}
My image model:
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);
为什么我收到一个空位置对象?
看下面的例子
person: id_person, name, id_country withRelated country: id_country, name, id_province withRelated province: id_province, name
OK生成模型人物、国家和省
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);
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);
with 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;
with controller person.js
'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"
}
}
},{...}]
}
你在模型中的关系是错误的。您可以将belongsToMany(仅用于m:n关系)与hasOne(不能与belongsToMany一起使用)结合使用。从你的问题中我不清楚这两个表之间有什么样的关系,所以我无法进一步帮助你。但问题不在于withRelated,而在于模型定义。
相关文章:
- 如何从类关系中检索对象列表
- 在Aurelia computeds中,当设置依赖关系时,如何声明对对象属性的依赖关系
- Angular 2,在没有直接关系的两个组件之间共享一个对象
- 经典方法的目的|与对象状态的关系
- 获取指向给定对象的所有解析关系
- 如何管理原始对象之间的数据依赖关系
- 嵌入多对多关系-如果一个新对象还不存在,则添加一个新的对象
- Javascript单元测试依赖关系-当依赖对象发生变化时,如何使测试失败
- 为什么没有将关系添加到模型中的 loopback.io 对象中
- 同一数组(AngularJS)中的javascript对象之间的一对一关系
- 在子对象中创建对父对象的引用是否会导致循环关系
- Parse.com 创建具有关系的对象
- Ember数据-类型错误:对象没有方法'每个关系'
- Parsejs angular/ionic在视图中检索关系对象(第二次打开页面时显示值)
- 将两个对象与第三个对象的关系进行比较
- 解析Javascript-如何通过关系对象进行查询's属性
- 在parse.com上的关系字段中重新查看对象
- Javascript对象关系
- Ember数据-过滤hasMany关系的对象
- Bookshelf js fetch withRelated选项返回空关系对象