带有猫鼬的羽毛 - 从多个集合中删除
Feathers with mongoose - delete from multiple collections
我试图让我的 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),对我出错的地方有帮助吗?
我想通了!
为了帮助他人,将 multi: true 添加到将删除多个实体的服务(在我的例子中是任务)很重要
我试图让我的 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),对我出错的地方有帮助吗?
我想通了!
为了帮助他人,将 multi: true 添加到将删除多个实体的服务(在我的例子中是任务)很重要