在stradi的管理用户界面中创建模型时,我有一些唯一的字段。
我意识到在api调用期间未提供field时,它将给出错误消息500
,而不是正确的错误消息。
我确实理解为什么会有错误,因为我可以在后端控制台中看到日志,并且已经扫描了诸如https://github.com/strapi/strapi/issues/1189和https://github.com/strapi/strapi/issues/1175之类的帖子
阅读这些问题后,我认为最好的方法是转到/api/controllers
并创建一个函数,例如create
来覆盖提供的函数,但出现错误Model.create is not a function
我在控制器中还没有做很多事情,所以代码很苗条。
module.exports = {
/* Strapi has default create function
* But because of the error message it provide is vague, will have to customize the controller */
create: async (ctx) => {
try {
console.log(ctx.request.body, 'ctx');
const article = await Article.create(ctx.request.body);
console.log(article, 'article');
} catch (e) {
console.log(e, 'error');
}
}
};
我已经阅读了发行票https://github.com/strapi/strapi/issues/1505
但是我正在使用绑架:3.0.0-beta.17.5节点:v10.17.0npm:6.11.3db:sqlite3(本地)PostgreSQL(分段)
有人知道我做错了什么吗?
提前感谢您的帮助和建议。
我建议您在此处检查默认控制器功能https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#extending-a-model-controller
您将看到如何使用服务功能来创建条目。
我不建议您使用模型全局变量。
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
module.exports = {
/**
* Create a record.
*
* @return {Object}
*/
async create(ctx) {
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
entity = await strapi.services.restaurant.create(data, { files });
} else {
entity = await strapi.services.restaurant.create(ctx.request.body);
}
return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};