如何防止服务的变化

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

我正在使用Mongoose的Feathers.js,我想创建一个服务无法更改的字段。

// 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个回答
1
投票

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

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

这个钩子将是beforecreateupdate方法的patch钩子。

© www.soinside.com 2019 - 2024. All rights reserved.