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?
3
Answers
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.
Happy coding 🙂
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].0 generate" for this old version.
If you are using the local mongodb service, locate the mongod.cfg file (usually in
Program FileMongoDBServer[version number]bin
) and configure it to use replica set. Add the following lines:Then launch
mongosh
from a terminal and initiate the replica set servers usingrs.initiate()
. Your code should be working fine now.