Search code examples
javascriptnext.jsprisma

Updating a prisma field using the "where" clause causes an error


I'm having this error when updating a field depending on the id of the model but I'm getting a Invalid prisma.resource.update() invocation: error.

This is my query:

const upvote = await prisma.resource.update({
  where: {
    id: body.resourceID,
  },
  data: {
    totalUpvotes: {
      increment: 1
    },
    userUpvoted: true,
  },
});

When I run the same query but use a string value instead of the body.resource variable,

const upvote = await prisma.resource.update({
  where: {
    id: "cl5njhgc90435e8sx2o6d",
  },
  data: {
    totalUpvotes: {
      increment: 1
    },
    userUpvoted: true,
  },
});

I'm getting the desired output. Can you tell me why this might be happening

I'm using NextJS, Prisma and PlanetScale for this application

Following is the full output of the error.

Request error PrismaClientValidationError:
Invalid `prisma.resource.update()` invocation:

{
  where: {
?   id?: String
  },
  data: {
    totalUpvotes: {
      decrement: 1
    },
    userUpvoted: false
  }
}

Argument where of type ResourceWhereUniqueInput needs at least one argument. Available args are listed in green.

Note: Lines with ? are optional.

    at Document.validate (C:\Users\Ammaar\resourcehub\node_modules\@prisma\client\runtime\index.js:48195:20)
    at PrismaClient._executeRequest (C:\Users\Ammaar\resourcehub\node_modules\@prisma\client\runtime\index.js:50632:17)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PrismaClient._request (C:\Users\Ammaar\resourcehub\node_modules\@prisma\client\runtime\index.js:50572:18)
    at async __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(api)/./pages/api/meta/downvote.js:17:28)
    at async Object.apiResolver (C:\Users\Ammaar\resourcehub\node_modules\next\dist\server\api-utils\node.js:179:9)
    at async DevServer.runApi (C:\Users\Ammaar\resourcehub\node_modules\next\dist\server\next-server.js:381:9)
    at async Object.fn (C:\Users\Ammaar\resourcehub\node_modules\next\dist\server\base-server.js:488:37)
    at async Router.execute (C:\Users\Ammaar\resourcehub\node_modules\next\dist\server\router.js:213:36) {
  clientVersion: '4.0.0'
}

Thanks in advance.


Solution

  • Based on the error you're receiving and your code, body.resourceID is being evaluated to undefined within your code. Make sure body.resourceID is a string by the time the code executes - is it possible your body contains a variable named something other than resourceID, and it's just a typo?