admin管理员组文章数量:1318765
I have an image model and a location model. The image model contains foreign key to location. To fetch the result I use:
fetch({withRelated: ['location']};
and i recieve the following results:
{
"id": 24,
"created_by": 1,
"location_id": 202,
"location": {}
}
But I want something like:
{
"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);
and my location model:
objectProperties = {
tableName: 'locations',
images: function () {
return this.belongsToMany(image, 'location_id');
}
};
classProperties = {};
locationModel = bookshelf.Model.extend(objectProperties, classProperties);
Why do I receive an empty location object?
I have an image model and a location model. The image model contains foreign key to location. To fetch the result I use:
fetch({withRelated: ['location']};
and i recieve the following results:
{
"id": 24,
"created_by": 1,
"location_id": 202,
"location": {}
}
But I want something like:
{
"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);
and my location model:
objectProperties = {
tableName: 'locations',
images: function () {
return this.belongsToMany(image, 'location_id');
}
};
classProperties = {};
locationModel = bookshelf.Model.extend(objectProperties, classProperties);
Why do I receive an empty location object?
Share Improve this question asked Jun 6, 2015 at 19:54 user2600769user2600769 1652 silver badges8 bronze badges 02 Answers
Reset to default 4look at the following example
person: id_person, name, id_country withRelated country: id_country, name, id_province and withRelated province: id_province, name
OK generate models person, country and province
person.js
'use strict'
const Bookshelf = require('../mons/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('../mons/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('../mons/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('../mons/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
});
});
}
the json is
{
"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"
}
}
},{...}]
}
Your relations in models are wrong. You use belongsToMany (used for m:n relationships only) in bination with hasOne (cannot be used with belongsToMany). It is not clear from your question what kind of relationship do these two tables have so i cannot assist you further. But the problem is not in withRelated but in model definition. Hope this helps.
本文标签: javascriptBookshelf js fetch withRelated option returns empty relation objectStack Overflow
版权声明:本文标题:javascript - Bookshelf js fetch withRelated option returns empty relation object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048293a2417922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论