admin管理员组

文章数量:1318563

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 0
Add a ment  | 

2 Answers 2

Reset to default 4

look 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