The problem:
I'm working on a React app w/ a Node Express Server and MongoDB. When trying to make a post or get request from the client-side, we receive a 500 status code and the following error:
Proxy error: Could not proxy request /api/workdays/allworkdays/5e5ee6a690f9a13de897d6a6 from localhost:3000 to http://localhost:3001/.
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).
In the case of the post request, the information is posted to the database though, eventually, we do receive the same error message along w/ a 500 status code.
In the case of the get request, the request is consoled in the back-end terminal but is never received by the browser. The network tab reflects a (pending) designation, like so:
Screenshot of network tab from browser
The intention of the get request is to query our Users collection - retrieve all linked workday ids, and then query the Workdays collection and retrieve all the documents w/ those ids.
The error message provides a link to an explanation of the error (ECONNRESET):
ECONNRESET (Connection reset by peer): A connection was forcibly closed by a peer. This normally results from a loss of the connection on the remote socket due to a timeout or reboot. Commonly encountered via the http and net modules.
We hope to take this response and push it to the state as events.
The workflow of our app is Component -> Axios Call -> API Routes -> Controller -> DB
Any help regarding this error would be especially appreciated as I'm a student looking to be employed soon. I'm doing this project, in addition to learning, for the purpose of including it in my portfolio.
Thanks
The error I'm getting:
Things I've tried:
React + Express: Proxy error: Could not proxy request /total from localhstem_errors
The code:
Package.json
{
"name": "mern",
"version": "1.0.0",
"description": "",
"main": "server.js",
"proxy": "http://localhost:3001",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"seed": "node scripts/seedDB.js",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"concurrently": "^4.1.2",
"nodemon": "^1.18.7"
},
"dependencies": {
"axios": "^0.18.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"if-env": "^1.0.4",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.1",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"react-big-calendar": "^0.24.0",
"react-moment": "^0.9.7",
"validator": "^12.2.0"
}
}
Component
import React, { Component } from "react";
import moment from "moment";
import { connect } from "react-redux";
import { Calendar, momentLocalizer } from "react-big-calendar";
import "react-big-calendar/lib/css/react-big-calendar.css";
import Modal from "../Modal";
import workdaysAPI from "../../utils/workdaysAPI";
import { toast } from "react-toastify";
import "./calendar.css";
const localizer = momentLocalizer(moment);
class MyCalendar extends Component {
componentDidMount() {
workdaysAPI
.getAllThisEmployeeWorkdays(this.props.auth.user.id)
.then(dbModel =>
this.setState({
events: dbModel
})
)
.catch(err => console.log(err));
}
constructor() {
super();
const events = [];, etc.
Axios Call
**import axios from "axios";
export default {
// Gets all workdays w/ user id
getAllThisEmployeeWorkdays: function(id) {
return axios.get("/api/workdays/allworkdays/" + id);
}
};**
API Routes
const router = require("express").Router();
const workdaysController = require("../../controllers/workdaysController");
const Workday = require("../../models/Workday");
// Matches with "/api/workdays"
router
.get("/allworkdays/:id", (req, res) => {
workdaysController.findAllById
});
Controller
const db = require("../models");
// Defining methods for the workdaysController
module.exports = {
findAllById: function(req, res) {
db.User
.findById({ _id: req.params.id })
.populate("workday")
.then(err, workday => {
db.Workday
.findById({ workday })
console.log("Returning all workdays ", workday.workday);
res.json(workday.workday)
})
.catch(err => res.status(422).json(err));
}, etc.
The Models
Users
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
role: {
type: String,
required: true
},
workday: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'workdays'
}],
schedule: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'schedules'
}]
});
module.exports = User = mongoose.model("users", UserSchema);
Workdays
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const WorkdaySchema = new Schema({
title: {
type: String,
required: true
},
availability: {
type: Boolean,
default: true
},
start: {
type: Date,
required: true
},
end: {
type: Date,
required: true
},
allDay: {
type: Boolean,
default: true
}
});
module.exports = Workday = mongoose.model("workdays", WorkdaySchema);