Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 271

mongo atlas model is present but the documents are not getting created

$
0
0

enter image description herei have started using next js lately and i'm new backend technologies , i have established the connection with monogdb atlas using moongose which works as expected but the post request date which is user obj which is getting sent but not created as document in collection , but the collection is getting created in the post request

//signup route.tsimport {connect} from '@/dbConfig/dbConfig';import User from '@/models/userModal.js';import bcrypt from 'bcryptjs'import { NextRequest, NextResponse} from 'next/server';connect()export async function POST(request:NextRequest){    try {        const reqBody =  await request.json()        const {username, email, password} = reqBody;        console.log(reqBody)        //check if user already exists         const user = await User.findOne({email})         if(user){            return NextResponse.json({error:"user already exists"},            {status:400})         }         //hash password         const salt = await bcrypt.genSalt(10)         const hashsedPassword = await bcrypt.hash         (password,salt)         const newUser = new user({            username,            email,            password:hashsedPassword        })        const savedUser = await newUser.save()        console.log(savedUser);        return NextResponse.json({            message:"user created succesfully",             success:true,            savedUser        },        {status:201})    } catch (error:any) {        return NextResponse.json({error:error.message},            {status:500})    }}
//dbConfig.tsimport mongoose from 'mongoose';export async function connect() {    try {        mongoose.connect(process.env.MONGO_URI!);        const connection = mongoose.connection;        connection.on('connected', () => {            console.log('MongoDB connected successfully');        })        connection.on('error', (err) => {            console.log('MongoDB connection error. Please make sure MongoDB is running. '+ err);            process.exit();        })    } catch (error) {        console.log('Something goes wrong!');        console.log(error);    }}

Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>