#user not saving on mongodb but works fine with mysql

3 messages · Page 1 of 1 (latest)

deep hinge
#

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
}```
and this is my controller ( using express and ts ):

```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}`;
  }
};```

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
deep hinge
#

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

sharp nacelleBOT