【问题标题】:How to prevent changes from services如何防止服务更改
【发布时间】:2017-12-24 01:41:45
【问题描述】:

我将 Feathers.jsMongoose 一起使用,并且我想创建一个服务无法更改的字段。

// account-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
const mongoose = require('mongoose');
require('mongoose-type-email');

module.exports = function(app) {
  const mongooseClient = app.get('mongooseClient');

  const recovery = new mongooseClient.Schema({
    token: { type: String, required: true }
  }, {
    timestamps: true
  });

  const account = new mongooseClient.Schema({
    firstName: { type: String, required: true },
    surname: { type: String, require: true },
    phone: { type: String, require: true },
    email: { type: mongoose.SchemaTypes.Email, required: true, unique: true },
    birth: { type: Date, required: true },
    gender: { type: String, required: true },
    country: { type: String, required: true },
    address: { type: String, required: true },
    address2: { type: String, required: false },
    city: { type: String, required: true },
    postcode: { type: String, required: true },
    password: { type: String, required: true },
    status: { type: String, required true }
  }, {
    timestamps: true
  });

  return mongooseClient.model('account', account);
};

没有人可以在/account/<id> 发帖并更改字段status。 此字段仅应在内部进行更改。当一些审批服务请求时。

如何实现这种行为?

【问题讨论】:

    标签: node.js mongodb validation mongoose feathersjs


    【解决方案1】:

    这是Feathers hooks 的完美用例。当从外部访问时,将在service method 调用中设置params.provider,以便您可以检查它并从data 中删除该字段(如果是):

    module.exports = function() {
      return async context => {
        if(context.params.provider) {
          delete context.data.status;
        }
      }
    }
    

    这个钩子将是 createupdatepatch 方法的 before 钩子。

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2011-10-12
      • 2019-07-09
      相关资源
      最近更新 更多