最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何防止服务的变化

运维笔记admin9浏览0评论

如何防止服务的变化

如何防止服务的变化

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

// account-model.js - A mongoose model
//
// See .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。该字段只能在内部更改。当一些批准服务请求。

我该如何实现这种行为?

回答如下:

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论