My front end has a link using axios with a placeID parameter: http://localhost:3001/api/getData?placeID=Uh8LPCRstLnJ-ZY3B41N9w
I am trying to retrieve results based on the placeID in my database. I have made sure that there is data with the specific placeID, but I keep getting a 404 not found error. What could be wrong with my get request?
This is what the back end code looks like:
const mongoose = require("mongoose");
const express = require("express");
var cors = require("cors");
const bodyParser = require("body-parser");
const logger = require("morgan");
const Data = require("./data");
const API_PORT = 3001;
const app = express();
app.use(cors());
const router = express.Router();
// MongoDB database connection
const dbRoute =
"mongodb+srv://name:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f888998b8bb89b948d8b8c9d8ac8d588c0998accd69597969f979c9ad6969d8c">[email protected]</a>/reviews?retryWrites=true";
// Connect backend code with the database
mongoose.connect(dbRoute, { useNewUrlParser: true });
let db = mongoose.connection;
db.once("open", () => console.log("connected to the database"));
// Check if connection with the database is successful
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// Parse request body to JSON format
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger("dev"));
// Get method to fetch data from the database
router.get("/getData/:placeID", (req, res) => {
const { placeID } = req.params.placeID;
Data.find({ placeID: placeID }, function(err, data) {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
});
});
// Launch backend on a port
app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));
This is the schema defined in the data.js file:
// /backend/data.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Define data structure for the database
const DataSchema = new Schema(
{
placeID: String,
seatRating: Number,
comfortRating: Number,
internetRating: Number,
noiseRating: Number,
outletRating: Number
},
{ timestamps: true }
);
module.exports = mongoose.model("Data", DataSchema);