如何为用户制作自定义路线?以及如何添加钩子?

问题描述 投票:0回答:1

我正在尝试添加路由/我以获取用户身份验证信息。这就是我的文件。我尝试在users.services文件中添加一个route / me,但是我收到此错误:“error:MethodNotAllowed:此端点不支持方法find。”

我希望得到一个用户对象(基于令牌)的响应到GET方法来路由'/ me'。

users.service.js

// Initializes the `users` service on path `/users`
const createService = require('feathers-sequelize');
const createModel = require('../../models/users.model');
const hooks = require('./users.hooks');

module.exports = function (app) {
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    name: 'users',
    Model,
    paginate
  };

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

  app.use('/me', {
      get(id, params) {
        return Promise.resolve([
          {
            id: 1,
            text: 'Message 1'
          }
        ])
      }
  })

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('users');

  service.hooks(hooks);
};

users.hooks.js

const { authenticate } = require('@feathersjs/authentication').hooks;

const {
  hashPassword, protect
} = require('@feathersjs/authentication-local').hooks;

module.exports = {
  before: {
    all: [ ],
    find: [ authenticate('jwt') ],
    get: [],
    create: [ hashPassword() ],
    update: [ hashPassword() ],
    patch: [ hashPassword() ],
    remove: []
  },

  after: {
    all: [ 
      // Make sure the password field is never sent to the client
      // Always must be the last hook
      protect('password')
    ],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};

users.model.js

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {

    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    },


  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  users.associate = function (models) { // eslint-disable-line no-unused-vars
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
  };

  return users;
};
javascript feathersjs
1个回答
3
投票

你做了什么

  app.use('/me', {
      get(id, params) {
        return Promise.resolve([
          {
            id: 1,
            text: 'Message 1'
          }
        ])
      }
  })

/me/:id的实施路线。 find方法适用于/me的基本路线。

我认为不需要单独的服务。一个更简单的解决方案是使用before all钩子,如果你访问/users/me改变id:

module.exports = function() {
  return async context => {
    if(context.id === 'me') {
      context.id = context.params.user._id;
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.