首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FeathersJS auk服务器填充钩子不工作

FeathersJS auk服务器填充钩子不工作
EN

Stack Overflow用户
提问于 2017-01-10 15:26:04
回答 1查看 566关注 0票数 2

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

我有一个与merchatTypeMap商人模型相关的1:1的模型,当我查询这个模型时,它应该使用附加的相关商人来回答。

merchantTypeMap服务:

代码语言:javascript
复制
'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);
};

这是我的merchantTypeMap模型代码:

代码语言:javascript
复制
'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服务挂钩:

代码语言:javascript
复制
'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: []
};

The 服务:

代码语言:javascript
复制
'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);
};

商人模型:

“严格使用”;

代码语言:javascript
复制
// 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;
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-10 16:15:59

您需要配置Sequelize来返回普通JS对象,而不是它的ORM对象,方法是使用raw: true选项。

顺便说一句,钩子v3很快就会发布。所有钩子,包括填充,然后将自动转换Sequelize和Mongoose为普通JS对象。对某些人来说,这可能是一个破缺。

相关:Sequelize - How can I return JSON objects of the database results only?

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41572336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档