Insert with custom id

Thanks for the quick reply.

I tried to implement the code in my controller (/post/controllers/post.js), but can’t make it work.

I don’t know what model and item should I pass in the filterModel function.

So at the moment this is the working controller with your first code.

const { parseMultipartData, sanitizeEntity } = require("strapi-utils");

module.exports = {
  async create(ctx) {
    let entity;
    if (ctx.is("multipart")) {
      // const { data, files } = parseMultipartData(ctx);
      // entity = await strapi.services.post.create(data, { files });
      return "multipart disabled";
    } else {
      // entity = await strapi.services.post.create(ctx.request.body);
      entity = await strapi
        .query("post")
        .model.forge(ctx.request.body)
        .save(null, { method: "insert" });
    }
    return sanitizeEntity(entity, { model: strapi.models.post });
  },
};

Works when I make new post api req { “id”: 1337, “name”: “leet” }, but not working when I include "tags: [1,3].

I’ve tried couple of ways to implement your last code (like this one below), but can’t make it work. (still learning js)

const { parseMultipartData, sanitizeEntity } = require("strapi-utils");

module.exports = {
  async create(ctx) {
    const filterModel = (model, item) => {
      let res = {};
      console.log(model._attributes);
      for (var i in model._attributes)
        if (item[i]) {
          if (model._attributes[i].type) {
            res[i] = item[i];
          } else if (model._attributes[i].collection) {
            res[i] = JSON.parse(item[i]);
          } else if (model._attributes[i].model) {
            res[i] = parseInt(item[i]);
          }
        }
      console.log(res);
      return res;
    };

    let entity;
    if (ctx.is("multipart")) {
      // const { data, files } = parseMultipartData(ctx);
      // entity = await strapi.services.post.create(data, { files });
      return "multipart disabled";
    } else {
      // entity = await strapi.services.post.create(ctx.request.body);
      entity = await strapi
        .query("post")
        .model.forge(filterModel(strapi.models.tag, ctx.request.body.tags))
        .save(null, { method: "insert" });
    }
    return sanitizeEntity(entity, { model: strapi.models.post });
  },
};