2

我试图使用新的填充钩子,但它似乎不起作用。我只是不知道我做错了什么。

我有merchatTypeMap模型与商家模型 1:1 相关,当我查询MerchantTypeMap时,它应该回答关联的商家。

商家类型地图服务:

'use strict';

const service = require('feathers-sequelize');
const merchantTypeMap = require('./merchantTypeMap-model');
const hooks = require('./hooks');

module.exports = function(){
  const app = this;

  const options = {
    Model: merchantTypeMap(app.get('sequelize')),
    paginate: {
      default: 5,
      max: 25
    }
  };

  // Initialize our service with any options it requires
  app.use('/merchantTypeMaps', service(options));

  // Get our initialize service to that we can bind hooks
  const merchantTypeMapService = app.service('/merchantTypeMaps');

  // Set up our before hooks
  merchantTypeMapService.before(hooks.before);

  // Set up our after hooks
  merchantTypeMapService.after(hooks.after);
};

这是我的MercerTypeMap模型代码:

'use strict';

// merchantTypeMap-model.js - A sequelize model
// 
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const merchantTypeMap = sequelize.define('merchantTypeMaps', {
    id: {
      type: Sequelize.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    merchantId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'merchants',
        key: 'id'
      },
      field: 'merchant_id'
    },
    merchantTypeId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'merchantTypes',
        key: 'id'
      },
      field: 'merchantType_id'
    }
  }, {
    freezeTableName: true
  });

  merchantTypeMap.belongsTo(sequelize.models.merchants, {foreignKey: 'merchant_id'});

  merchantTypeMap.sync();

  return merchantTypeMap;
};

这里我的merchantTypeMap 服务挂钩:

'use strict';

const globalHooks = require('../../../hooks');
const hooks = require('feathers-hooks-common');

const schema = {
  include: [{
    service: 'merchants',
    nameAs: 'merchantItem',
    parentField: 'merchantId',
    childField: 'id',
  }]
};

exports.before = {
  all: [],
  find: [],
  get: [],
  create: [],
  update: [],
  patch: [],
  remove: []
};

exports.after = {
  all: [],
  find: [
    hooks.populate( { schema } )
  ],
  get: [
    hooks.populate( { schema } )
  ],
  create: [],
  update: [],
  patch: [],
  remove: []
};

商家服务:

'use strict';

const service = require('feathers-sequelize');
const merchantTypeMap = require('./merchantTypeMap-model');
const merchants = require('./merchants-model');
const hooks = require('./hooks');

module.exports = function(){
  const app = this;

  merchantTypeMap(app.get('sequelize'));

  const options = {
    Model: merchants(app.get('sequelize')),
    paginate: {
      default: 5,
      max: 25
    }
  };

  // Initialize our service with any options it requires
  app.use('/merchants', service(options));

  // Get our initialize service to that we can bind hooks
  const merchantsService = app.service('/merchants');

  // Set up our before hooks
  merchantsService.before(hooks.before);

  // Set up our after hooks
  merchantsService.after(hooks.after);
};

商家模型:

'使用严格';

// merchants-model.js - A sequelize model
// 
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const merchants = sequelize.define('merchants', {
    id: {
      type: Sequelize.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    title: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'title'
    },
    latitude: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'latitude'
    },
    longitude: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'latitude'
    },
    address: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'address'
    },
    icon: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'icon'
    },
    zoneId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'zones',
        key: 'id'
      },
      field: 'zone_id'
    }
  }, {
    freezeTableName: true
  });

  merchants.belongsToMany(sequelize.models.merchantTypes, {through: sequelize.models.merchantTypeMaps});

  merchants.sync();

  return merchants;
};
4

1 回答 1

4

raw: true您需要使用该选项将 Sequelize 配置为返回普通的 JS 对象,而不是其 ORM 对象。

顺便说一句,钩子 v3 将很快发布。所有的钩子,包括填充,都会自动将 Sequelize 和 Mongoose ORM 转换为普通的 JS 对象。对某些人来说,这可能是一次重大变革。

相关:Sequelize - 如何仅返回数据库结果的 JSON 对象?

[编辑] 从 ORM 对象的转换很可能在 Feathers 服务中完成,而不是通过钩子完成。这样,人们编写的自定义钩子就不会出现问题。

于 2017-01-10T16:15:59.507 回答