Jaden derulo
Jaden derulo

Reputation: 251

Prisma Mongodb can't create a user model

So I am using Prisma for the first time and my provider is mongodb and when I want to create a model it throws me an error

Invalid `prisma.user.create()` invocation:


  Prisma needs to perform transactions, which requires your MongoDB server to be run 
as a replica set. https://pris.ly/d/mongodb-replica-set

I am using Prisma in a nextjs app and I put the code inside the API pages

My DATABASE_URL is mongodb://localhost:27017/threadzees

Code :

await prisma.user.create({
      data: {
        username,
        email,
        avatar: "1",
        createdAt: new Date(),
      },
    });

How do I fix this issue?

Upvotes: 7

Views: 10520

Answers (5)

shrthk7
shrthk7

Reputation: 21

If you are using the local mongodb service, locate the mongod.cfg file (usually in Program File\MongoDB\Server\[version number]\bin) and configure it to use replica set. Add the following lines:

replication:
   replSetName: rs0

Then launch mongosh from a terminal and initiate the replica set servers using rs.initiate(). Your code should be working fine now.

PS:

  • For unix systems you can find mongod.conf in /etc/ directory.
  • For MacOS if installed mongo using brew you can find it in /opt/homebrew/etc/

Upvotes: 2

Satyam Raj
Satyam Raj

Reputation: 1

See the straight forward answer is that just don't use local mongodb url.

Upvotes: 0

ziyad anbari
ziyad anbari

Reputation: 1

just replace localhost with 127.0.0.1

mongodb://127.0.0.1:27017/threadzees

Upvotes: 0

Ihar
Ihar

Reputation: 5

You can use "prisma": "2.26.0" and "@prisma/client": "2.26.0" instead of your current version. They don`t need a replica set. Also you have to use @default(dbgenerated()) instead of @default(auto()) both with "npx [email protected] generate" for this old version.

Upvotes: 0

Ninja Master
Ninja Master

Reputation: 186

I'm Running Mongodb 4+ version I solved it as below As the error describes you need to create a replica. Either you can use cloud based Mongo or locally you can create a replica like below.

# Open new terminal execute below command
 mongod --port=27001 --dbpath=. --replSet=rs0
# Open another terminal window execute below command
mongo.exe
# Then below command
rs.initiate( {    _id : "rs0", members: [ { _id: 0, host: "localhost:27001" } ] })
# your new connection String
mongodb://localhost:27001
  1. --dbpath : Is your data path directory (above command points to current directory)
  2. --replSet=rs0 : Replica name
  3. Read more @ https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/

Happy coding :)

Upvotes: 6

Related Questions