Strapi error for backend customization: Undefined attribute level operator id

System Information
  • Strapi Version: 4.1.12
  • Operating System: Linux-5.16.20-051620-generic-x86_64-with-glibc2.29
  • Database: Postgresql
  • Node Version: v16.14.2
  • NPM Version: 8.5.0
  • Yarn Version: 1.22.18

DESCRIPTION

I am building a custom controller for profile api but it is giving me a lot of error.

The Code

"use strict";

/**
 *  profile custom controller
 */

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::profile.profile", ({ strapi }) => ({
  async createMe(ctx) {
    try {
      let entity;

      // Fetching authenticated users details
      const user = ctx.state.user;

      if (!user) {
        return ctx.badRequest(null, [
          { message: [{ id: "No autorization token was found." }] },
        ]);
      }

      const data = ctx.request.body;
      data["user"] = user;
      entity = await strapi.entityService.create("api::profile.profile", {
        data: data,
      });

      const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
      return this.transformResponse(sanitizedEntity);
    } catch (error) {
      return ctx.badRequest("Internal API error", { realError: error });
    }
  },
  async getMe(ctx) {
    let entities;
    const user = ctx.state.user;

    if (!user) {
      return ctx.badRequest(null, [
        { message: [{ id: "No autorization token was found." }] },
      ]);
    }

    var __user = {
      data: {
        id: ctx.state.user.id,
        attributes: {
          username: ctx.state.user.username,
          email: ctx.state.user.email,
          provider: ctx.state.user.provider,
          confirmed: ctx.state.user.confirmed,
          blocked: ctx.state.user.blocked,
          createdAt: ctx.state.user.createdAt,
          updatedAt: ctx.state.user.updatedAt,
          beta: ctx.state.user.beta,
        },
      },
    };

    entities = await strapi.db.query("api::profile.profile").findMany({
      where: {
        $and: [
         {
           user: __user
         },

        ],
      },
    });

    return entities.map(async (entity) => {
      await this.sanitizeOutput(entity, ctx);
    });
  },
  async updateMe(ctx) {
    return ctx.badRequest("Internal Server Error", {
      message: "This api endpoint is under construction.",
    });
  },
}));

ERROR

While fetching http://localhost:1337/api/profiles/me/get (Uses the getMe function as mentioned above.) it is returning

error: Undefined attribute level operator id
Error: Undefined attribute level operator id
    at processAttributeWhere (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:82:13)
    at processWhere (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:142:45)
    at processNested (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:112:12)
    at processWhere (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:155:25)
    at processNested (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:112:12)
    at /workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:126:39
    at Array.map (<anonymous>)
    at processWhere (/workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:126:28)
    at /workspace/strapi-backened/backened/node_modules/@strapi/database/lib/query/helpers/where.js:104:29
    at Array.map (<anonymous>)

Hi! Could you solve this problem?

This may or may not help but I got the same error when I tried to use an array in a query where a single entity was expected. Check your code, maybe there’s an array somewhere you think it’s not.

Try achieving this using the entity service API. You may want to check this out: Entity Service API - Strapi Developer Docs

I’m getting a similar error and it’s the same whether I use the Entity Service or the Query Service

@ SteveFunso There is not a single example of populate with neither a create or update command, find sure. The documentation needs to be updated with actual examples for those methods

2 Likes

It would be really helpful already if the typescript typings had some information about what to provide where.

Trying

await strapi.entityService.findOne(
    "plugin::users-permissions.user",
    {
      populate: "*",
    },
    ctx.params.id
  );

and getting Undefined attribute level operator populate. The docs show only how to use populate for findMany: Entity Service API - Populating - Strapi Developer Docs

EDIT: It was the other way round:

  let affectedRecordData = await strapi.entityService.findOne(
    "plugin::users-permissions.user",
    ctx.params.id,
    {
      populate: "*",
    }
  );

For me this issue was that one of our developers changed our find and findOne controller code and replaced super.findOne with strapi.service("api:blabla").findOne(sanitizedQueryParams) and made a mistake by not passing the id into the findOne like so: await strapi.service("api::ticket.ticket").findOne(id, sanitizedQueryParams) . Silly mistake but luckily our tests caught it.

The error from strapi’s side needs to be improved.

As for me I have to specify the request data before sending it

:x: Not working

const profile = await strapi.db.query("api::profile.profile").findOne({
    where: {
        user: ctx.state.user,
    },
    select: ["id"],
});

:white_check_mark: Works fine

const profile = await strapi.db.query("api::profile.profile").findOne({
    where: {
        user: {
            id: ctx.state.user.id,
        }
    },
    select: ["id"],
});