Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Model.create不是函数-Stradi(Model.create is not a function - strapi)

I have a few unique fields when creating model in strapi's admin ui.

(在Stradi的管理ui中创建模型时,我有几个独特的字段。)

I realized that when field is not provided during the api call, it'll give an error message of 500 instead of the proper error message.

(我意识到当在api调用期间未提供field时,它将给出一条错误消息500而不是正确的错误消息。)

I do understand why there's an error since I can see the log in backend console and I have scanned through posts such as https://github.com/strapi/strapi/issues/1189 and https://github.com/strapi/strapi/issues/1175

(我确实了解为什么会出错,因为我可以在后端控制台中看到日志,并且已经浏览了https://github.com/strapi/strapi/issues/1189https://github.com/strapi/绑架/问题/ 1175)

After reading those issues, I believe the best way is to go /api/controllers and create a function such as create to override the one provided but I get an error of Model.create is not a function

(阅读完这些问题后,我相信最好的方法是转到/api/controllers并创建一个函数(例如create以覆盖提供的函数,但是出现Model.create is not a function错误, Model.create is not a function)

I have not do much in the controller yet so code is slim.

(我在控制器中没有做太多事情,因此代码很苗条。)

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');
        }
    }
};

I have read the issue ticket https://github.com/strapi/strapi/issues/1505 But I am using strapi: 3.0.0-beta.17.5 node: v10.17.0 npm: 6.11.3 db: sqlite3 (local) postgresql (staging)

(我已经阅读了问题单https://github.com/strapi/strapi/issues/1505但我使用的是trapi:3.0.0-beta.17.5节点:v10.17.0 npm:6.11.3 db:sqlite3(本地) PostgreSQL(暂存))

Anyone know what I might have done wrong?

(有人知道我做错了吗?)

Thanks in advance for any help and advise.

(在此先感谢您的帮助和建议。)

  ask by Dora translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you check the default controller functions here https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#extending-a-model-controller

(我建议您在这里检查默认控制器功能https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#extending-a-model-controller)

You will see how to use service functions to create entries.

(您将看到如何使用服务功能来创建条目。)

I don't suggest you to use Model Global variables.

(我不建议您使用Model Global变量。)

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 });
  },
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...