书架获取相关数据属性为空

Bookshelf get related data attributes is empty

我似乎无法从相关模型中检索属性。

型号:

// DB = export of Bookshelf

// Signup model
var DB = require('../db').DB;
var UserMeta = require('./usersmeta');

var Signup = DB.Model.extend({
  tableName: 'tblSignups',
  idAttribute: 'id',
  hasTimestamps: true,
  usermeta: function() {
    return this.belongsTo(UserMeta);
  }
});

module.exports = Signup;

// Usermeta model
var DB = require('../db').DB;
var User = require('./users');

var UserMeta = DB.Model.extend({
  tableName: 'tblUsersMeta',
  idAttribute: 'id',
  hasTimestamps: true,
  user: function() {
    return this.belongsTo(User);
  }
});

module.exports = UserMeta;

管理员注册路径:

router.get('/signups', function(req, res, next) {
  model.SignUp.fetchAll({withRelated: ['usermeta']}).then(function(collection) {
    for(var i = 0; i < collection.toArray().length; i++){
      console.log('Signup ' + i + '\n--------------------------');
      console.log(collection.toArray()[i]);
      console.log('Related usermeta\n--------------------------');
      console.log(collection.toArray()[i].related('usermeta'));
    }
    res.render('admin/pages/signups', {title: 'signups', error: false, signups: collection.toArray()});
  }).catch(function(err) {
    res.status(500).json({error: true, data: {message: err.message}});
  });
});

这是控制台日志显示的内容:

Signup 0
--------------------------
ModelBase {
  attributes:
   { id: 2,
     usermeta_id: 5,
     state: 'pending',
     created_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time),
     updated_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time) },
  _previousAttributes:
   { id: 2,
     usermeta_id: 5,
     state: 'pending',
     created_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time),
     updated_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time) },
  changed: {},
  relations:
   { usermeta:
      ModelBase {
        attributes: {},
        _previousAttributes: {},
        changed: {},
        relations: {},
        cid: 'c38',
        relatedData: [Object] } },
  cid: 'c35',
  id: 2 }
Related usermeta
--------------------------
ModelBase {
  attributes: {},
  _previousAttributes: {},
  changed: {},
  relations: {},
  cid: 'c38',
  relatedData:
   RelationBase {
     targetTableName: 'tblUsersMeta',
     targetIdAttribute: 'id',
     type: 'belongsTo',
     target:
      { [Function]
        NotFoundError: [Function: ErrorCtor],
        NoRowsUpdatedError: [Function: ErrorCtor],
        NoRowsDeletedError: [Function: ErrorCtor] },
     foreignKey: 'tblUsersMetum_id',
     parentId: undefined,
     parentTableName: 'tblSignups',
     parentIdAttribute: 'id',
     parentFk: undefined } }

如您所见,相关模型属性为空。我按如下方式保存了我的 usermeta:

userMeta.save().then(function(usermeta) {
        var signup = new model.SignUp({
          usermeta_id: usermeta.attributes.id
        });
        signup.save().then(function() {
          req.flash('success', 'Success!');
          res.redirect('/signup');
        }).catch(function(err) {
          res.status(500).json({error: true, data: {message: err.message}});
        });
      }).catch(function (err) {
        res.status(500).json({error: true, data: {message: err.message}});
      });

在数据库中,usermeta 的 ID(在注册 table 中)设置正确。我在这里做错了什么?

您的模型似乎有问题。当您设置模型之间的关系时,您在两个示例中都使用了 belongsTo - 这是不对的。我不知道它们之间的关系是什么(多对多?一对多?)。如果它是多对多你需要使用 belongsToMany 如果它是一对多你需要在数据库中不保存外键的模型上设置 hasManybelongsTo 到数据库中有外键的模型。

看到这个http://bookshelfjs.org/#one-to-many and this http://bookshelfjs.org/#many-to-many

希望对您有所帮助!