By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Not able to POST http://localhost:5000/api/createuser
JavaScript

Javascript – Not able to POST http://localhost:5000/api/createuser

Admin
Last updated: 2023/12/19 at 5:37 PM
Admin
Share
3 Min Read
Not able to POST http://localhost:5000/api/createuser

Problem:

While pursuing a MERN stack project tutorial on YouTube, I’ve encountered an obstacle in the midst of my progress. The specific issue revolves around the implementation of user registration, a recent addition to my project. I’ve been using ThunderClient to test this feature, and it seems to be causing a challenge. To address this problem effectively, I would appreciate guidance and assistance, particularly in the form of error messages or code snippets related to the registration process.

Contents
Problem:Solution:

enter image description here

the result should have been success But I am getting error please help! 🙂

this is the main index file
Index.js

const express = require('express')
const app = express()
const port = 5000
const mongoDB = require("./db")

app.get('/', (req, res) =\> {
res.send('Hello World!')
})
app.use(express.json());
app.use('/api', require("./Routes/CreateUser"));
app.listen(port, () =\> {
console.log(`Example app listening on port ${port}) })

this is the db file
db.js

const mongoose = require('mongoose');
const mongoURI = 'mongodb+srv://Nikita:shani@cluster0.e8wdets.mongodb.net/?retryWrites=true&w=majority'
const mongoDB =async()=>{
   await mongoose.connect(mongoURI, { useNewUrlParser: true }, async (err, result) => {
        if (err) console.log("---", err);
        else {
            console.log("connected");
            const fetched_data = await mongoose.connection.db.collection("food_items");
            fetched_data.find({}).toArray(function (err, data) {
                if (err) console.log(err);
                else console.log(data);

            });

        }

    });
}

module.exports = mongoDB;


this is the user file
User.js

const mongoose = require('mongoose')

const { Schema } = mongoose;

const UserSchema = new Schema({
    name:{
        type:String,
        required:true
    },
    location:{
        type:String,
        required:true,
    },
    email:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required:true
    },
    date:{
        type:Date,
        default:Date.now
    },

  });

  module.exports = mongoose.model('user',UserSchema)

this is the sample file to check whether the data is being stored in the database or not
CreateUser.js

const express = require('express')
const router = express.Router()
const User = require('../models/User')

router.post("/creatuser", async (req, res)=>{
    try {
        await User.create({
            name: "Shyam Das",
            password: "123456",
            email: "shyamdas12@hotmail.com", 
            location: "Qwerty edrfef"
        })
    res.json({success:true});    

    } catch (error) {
        console.log(error);
        res.json({success:false});
    }
})

module.exports = router;

Solution:

In the CreateUser.js file there is a typo error:

router.post("/creatuser", async (req, res)=>{...} // You wrote "creatuser"
router.post("/createuser", async (req, res)=>{...} // Change it to "createuser"

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article I need to add social media images in the html string I have in the footer of my site done with js [closed] Javascript – I need to add social media images in the html string I have in the footer of my site done with js [closed]
Next Article Type Error for JS library project & DOM Traversal issues Javascript – Type Error for JS library project & DOM Traversal issues
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?