strapi delete文件有问题。
我可以上传文件。如果我想删除一个上传的文件,它会返回错误:"内部服务器错误"从响应中删除,但文件已被删除。
我哪里做错了?
错误日志:
[2021-10-21T19:37:46.827Z] error TypeError: targetModel.updateMany is not a function
at C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-connector-mongooselibrelations.js:487:38
at CoreDocumentArray.map (<anonymous>)
at CoreDocumentArray.map (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-connector-mongoosenode_modulesmongooselibtypesdocumentarray.js:42:27)
at C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-connector-mongooselibrelations.js:464:42
at Array.map (<anonymous>)
at Function.deleteRelations (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-connector-mongooselibrelations.js:393:25)
at deleteOne (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-connector-mongooselibqueries.js:560:17)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Object.delete (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-databaselibquerieshelpers.js:15:18)
at async destroy (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-plugin-uploadcontrollersuploadapi.js:55:5)
at async C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapilibmiddlewaresrouterutilsrouterChecker.js:79:22
at async module.exports (C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-plugin-users-permissionsconfigpoliciespermissions.js:88:3)
at async C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapi-utilslibpolicy.js:68:5
at async C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapilibmiddlewaresparserindex.js:48:23
at async C:Usersgizem.ayDocumentsProjectspersonalwaa-backendnode_modulesstrapilibmiddlewaresxssindex.js:26:9
[2010-10-21 t19:37:46. 830z] debug DELETE/upload/files/6171c17b24b4715ba01791af (3267 ms) 500
项目出错"TypeError: targetModel。updateMany不是一个函数">
React js中的代码:
export const deleteFileById = async (fileId) => {
return await axios
.delete(`http://localhost:1337/upload/files/${fileId}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
}
Strapi中的代码Strapi -connector-mongooselib relationships .js:487:38:if (Array.isArray(entry[association.alias])) {
return Promise.all(
entry[association.alias].map(val => {
const targetModel = strapi.db.getModelByGlobalId(val.kind);
// console.log(targetModel);
// ignore them ghost relations
if (!targetModel) return;
const field = val[association.filter];
const reverseAssoc = targetModel.associations.find(
assoc => assoc.alias === field
);
if (reverseAssoc && reverseAssoc.nature === 'oneToManyMorph') {
return targetModel.updateMany(
{
[targetModel.primaryKey]: val.ref && (val.ref._id || val.ref),
},
{
[field]: null,
},
{ session }
);
}
return targetModel.updateMany(
{
[targetModel.primaryKey]: val.ref && (val.ref._id || val.ref),
},
{
$pull: { [field]: primaryKeyValue },
},
{ session }
);
})
);
}
还有我的包裹。
"dependencies": {
"knex": "0.21.18",
"mongodb": "^4.1.3",
"sqlite3": "5.0.0",
"strapi": "3.6.8",
"strapi-admin": "3.6.8",
"strapi-connector-bookshelf": "3.6.8",
"strapi-connector-mongoose": "^3.6.8",
"strapi-connector-mongoose-modified": "^3.1.9",
"strapi-hook-mongoose": "^3.0.0-beta.17.8",
"strapi-plugin-content-manager": "3.6.8",
"strapi-plugin-content-type-builder": "^3.6.8",
"strapi-plugin-email": "3.6.8",
"strapi-plugin-i18n": "3.6.8",
"strapi-plugin-upload": "3.6.8",
"strapi-plugin-users-permissions": "3.6.8",
"strapi-utils": "3.6.8"
},
有一个优雅的解决方案(稍微修复了代码):
- 安装包
npm i patch-package -D
- 添加脚本到package.json
- 在Strapi根目录下创建一个文件夹,文件为
补丁/strapi-connector-mongoose + 3.6.8.patch
- 打开strapi-connector-mongoose+3.6.8补丁文件,粘贴以下内容,保存并关闭文件(格式和缩进应该像这样)
diff --git a/node_modules/strapi-connector-mongoose/lib/relations.js b/node_modules/strapi-connector-mongoose/lib/relations.js
index 411046a..a0c535e 100644
--- a/node_modules/strapi-connector-mongoose/lib/relations.js
+++ b/node_modules/strapi-connector-mongoose/lib/relations.js
@@ -473,7 +473,7 @@ module.exports = {
);
if (reverseAssoc && reverseAssoc.nature === 'oneToManyMorph') {
- return targetModel.updateMany(
+ return strapi.db.getModel(targetModel.modelName).updateMany(
{
[targetModel.primaryKey]: val.ref && (val.ref._id || val.ref),
},
@@ -484,7 +484,7 @@ module.exports = {
);
}
- return targetModel.updateMany(
+ return strapi.db.getModel(targetModel.modelName).updateMany(
{
[targetModel.primaryKey]: val.ref && (val.ref._id || val.ref),
},
- 现在我们进入
- 启动Strapi并测试图像删除
npm我