Bookshelf js fetch withRelated option returns 空关系对象

Bookshelf js fetch withRelated option returns empty relation object

我有一个图像模型和一个位置模型。图像模型包含位置的外键。要获取我使用的结果:

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);

为什么我收到一个空的位置对象?

你在模型中的关系是错误的。您将 belongsToMany(仅用于 m:n 关系)与 hasOne(不能与 belongsToMany 一起使用)结合使用。从你的问题中不清楚这两个表有什么样的关系,所以我无法进一步帮助你。但问题不在于 withRelated,而在于模型定义。希望这有帮助。

看下面的例子


个人:id_person,姓名,id_country withRelated 国家:id_country,姓名,id_province and withRelated 省份: id_province, name

确定生成人物、国家和省份模型

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);

有合集person.js

'use strict'
const Bookshelf = require('../commons/bookshelf');
const Person = require('../models/person');
const Persons = Bookshelf.Collection.extend({
    model: Person
});
module.exports = Persons;

带控制器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"
                 }
           }
    },{...}]
}