P
Prisma7mo ago
jey

user not saving on mongodb but works fine with mysql

hi , i was using mysql and switched to test mongodb , followed the docs on mongodb and my User model looks as follow now :
enum Role {
USER
MOD
ADMIN
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
password String
subscriptionNumber String?
firstName String
lastName String
phone String?
isAccepted Boolean @default(false)
role Role
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum Role {
USER
MOD
ADMIN
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
password String
subscriptionNumber String?
firstName String
lastName String
phone String?
isAccepted Boolean @default(false)
role Role
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
and this is my controller ( using express and ts ):
export const registerUser = async (req: Request, res: Response) => {
const { email, password, firstName, lastName, role, phone } = req.body;

try {
const salt = await bcrypt.genSalt(10);
const photo = req.file ? req.file.filename : 'default.jpg';
const userData: any = {
email: email,
password: await bcrypt.hash(password, salt),
firstName: firstName,
lastName: lastName,
role: role,
photo: photo,
phone: phone,
};

const user = await prisma.user.create({
data: userData,
});
console.log(user);
res.json(ok('NEW_USER_ADDED'));
} catch (err: any) {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
if (err.code === 'P2002') {
res.status(400).json(error('EMAIL_IN_USE'));
}
}
return `${err.message}`;
}
};
export const registerUser = async (req: Request, res: Response) => {
const { email, password, firstName, lastName, role, phone } = req.body;

try {
const salt = await bcrypt.genSalt(10);
const photo = req.file ? req.file.filename : 'default.jpg';
const userData: any = {
email: email,
password: await bcrypt.hash(password, salt),
firstName: firstName,
lastName: lastName,
role: role,
photo: photo,
phone: phone,
};

const user = await prisma.user.create({
data: userData,
});
console.log(user);
res.json(ok('NEW_USER_ADDED'));
} catch (err: any) {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
if (err.code === 'P2002') {
res.status(400).json(error('EMAIL_IN_USE'));
}
}
return `${err.message}`;
}
};
i'm sure the code is fine but still i have no idea what's going on wrong in here , i did push and generate my prisma and i see the tables fine in my mongodb compass any help is appreciated
Solution:
issue was with Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set configured my mongo and it worked fine...
Jump to solution
2 Replies
Solution
jey
jey7mo ago
issue was with Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set configured my mongo and it worked fine
Nurul
Nurul7mo ago
Replica set is usually configured by default if you are using MongoDB Atlas. Were you using some other hosted services for your mongo database?
Want results from more Discord servers?
Add your server