我正在尝试让我的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),对我要去哪里的任何帮助?
答案 0 :(得分:0)
我想通了!
为了帮助他人,重要的是添加multi:对于要删除多个实体(在我的情况下为任务)的服务而言,是正确的