javascript - Bookshelf js fetch withRelated选项返回空关系对象

标签 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,
        "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);

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

最佳答案

看下面的例子


person: id_person, name, id_country withRelated country: id_country, name, id_province and withRelated province: id_province, name

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

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

国家.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);

省.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;

带有 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"
                 }
           }
    },{...}]
}

关于javascript - Bookshelf js fetch withRelated选项返回空关系对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686977/

相关文章:

nhibernate - 使用 Fluent NHibernate/Nhibernate 和 Automapping 快速加载

php - laravel 5.1 以多对多关系获取每个类别的 5 个新闻

javascript - Jquery Fancyform 和 IE10 : Checkbox does not get checked when clicking on label

javascript - Firebase:通过 firebase.db().ref ('myRef' ) 和 firebase.db.ref ('myRef' ) 检索的数据之间的差异。once ('value' ,回调)

javascript - 与 Thymeleaf 一起使用时切换按钮出现问题

node.js - 如何使用 redis 适配器覆盖套接字的基本适配器?

javascript - 如何检查值是否是 Angular 模板中的对象?

node.js - CircleCI:构建 Meteor 2.2 docker 应用程序 - 失败

node.js - 访问 Node 中的数据字段给我未定义