带有猫鼬的羽毛-从多个收藏夹中删除

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

我正在尝试让我的feather.js api删除该用户所属的所有任务。已删除。最终,我试图建立一个删除帐户功能

用户模型:

// users-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
module.exports = function (app) {
  const modelName = 'users';
  const mongooseClient = app.get('mongooseClient');
  const schema = new mongooseClient.Schema({

    email: { type: String, unique: true, lowercase: true },
    password: { type: String },
    firstName: { type: String, required: true },
    lastName: { type: String, required: true }
  }, {
    timestamps: true
  });

  // This is necessary to avoid model compilation errors in watch mode
  // see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
  if (mongooseClient.modelNames().includes(modelName)) {
    mongooseClient.deleteModel(modelName);
  }
  return mongooseClient.model(modelName, schema);

};

任务模型:

module.exports = function (app) {
  const modelName = 'tasks';
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;
  const schema = new Schema(
    {
      text: { type: String, required: true },
      description: { type: String },
      completed: { type: Boolean, default: false },
      dueAt: { type: Date, default: null},
      user: {
        type: Schema.Types.ObjectId,
        ref: 'users',
      },
    },
    {
      timestamps: true,
    }
  );

  // This is necessary to avoid model compilation errors in watch mode
  // see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
  if (mongooseClient.modelNames().includes(modelName)) {
    mongooseClient.deleteModel(modelName);
  }
  return mongooseClient.model(modelName, schema);
};

在我的users.hooks.js中,我尝试在以下部分中添加以下内容:

 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: [],
    async remove(context) {
      const user = context.result;

      await context.app.service("tasks").remove(null, {
        query: { user: user._id },
      });
    },
  },

[当我尝试执行此操作时,它为我提供了不允许的错误方法(405),有关我要去哪里的任何帮助?

node.js rest mongoose feathersjs
1个回答
0
投票

我想通了!

为了帮助他人,重要的是添加multi:对于要删除多个实体(在我的情况下是任务)的服务,true)>

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