0

Struggling to get my data to post - I'm getting a 404 error. I've looked around the forum's and I think it has something to do with my route. I'm not sure I understand the routes aspect and how that works exactly. My get function works fine and pulls the data from the database. Any input would be helpful, I've been stuck on this one for a bit now and not sure where my issue is. Thanks everyone!

I have a database called test with a collection called workorders in it (Not sure if that helps or not)

server.js

const express = require('express')
const connectDB = require('./db.js')
const itemModel = require('./models/item.js')
const cors = require('cors')

const app = express()
app.use(express.json())
app.use(cors())
connectDB()

app.get('http://localhost:3001', async (req, res) => {
    const response = await itemModel.find()
    return res.json({workorders : response})
})

app.post('http://localhost:3001', (req, res) => {
    console.log(req.body)
})


app.listen(3001, () => {
    console.log("app is running");
}) 

app.js (post function - Called onClick )

  const postData = (e) => {
    axios
      .post("http://localhost:3001", {
        customerName: 'Test',
        jobDescription: 'Test',
        jobPrice: 'Test',
      })
      .then((response) => {console.log(response.data)})
      .catch((err) => {
        console.log(err);
      });
  };

item.js (object schema)


const itemSchema = new mongoose.Schema({
    customerName: String,
    jobDescription: String,
    jobPrice: String,
    laborNotes: String,
    laborType: String,
    IPWO: String,
    refWO: String,
    materialStatus: String,
    dateScheduled: Date,
    woStatus: String,
    notes: String
})

const itemModel = mongoose.model("workorders", itemSchema)
module.exports = itemModel

2
  • 1
    the parameter to app.post() is a route/path, not a full URL.
    – Evert
    Commented Jul 10 at 1:51
  • Based on your current set up it will be app.get('/', async (req, res) => {...}) and app.post('/', async (req, res) => {...}). Your server is already listening on port 3001 so you don't need to specify the full url, only relative to the web root of the server which will be /.
    – jQueeny
    Commented Jul 10 at 8:58

1 Answer 1

1

You have to give route path to the app.post not a full url.

So server.js would be.

const express = require('express');
const connectDB = require('./db.js');
const itemModel = require('./models/item.js');
const cors = require('cors');

const app = express();
app.use(express.json());
app.use(cors());
connectDB();
 
app.get('/items', async (req, res) => { // for get items
    const response = await itemModel.find();
    return res.json({ workorders: response });
});

app.post('/item',async (req, res) => { // for create item
    console.log(req.body);
});

app.listen(3001, () => {
    console.log('app is running');
}); 

And you can get use these two endpoint called items to get list of orders and item for save order

//For post
const postData = e => {
    axios
        .post('http://localhost:3001/item', {
            customerName: 'Test',
            jobDescription: 'Test',
            jobPrice: 'Test'
        })
        .then(response => {
            console.log(response.data);
        })
        .catch(err => {
            console.log(err);
        });
};



//For get items
const postData = e => {
    axios
        .get('http://localhost:3001/items')
        .then(response => {
            console.log(response.data);
        })
        .catch(err => {
            console.log(err);
        });
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.