Node.js React/Express应用程序-API请求到后端代理错误:无法代理请求(EconReset)
问题: 我正在开发一款带有Node Express服务器和MongoDB的React应用程序。当尝试从客户端发出post或get请求时,我们收到500状态码和以下错误:Node.js React/Express应用程序-API请求到后端代理错误:无法代理请求(EconReset),node.js,reactjs,mongodb,Node.js,Reactjs,Mongodb,问题: 我正在开发一款带有Node Express服务器和MongoDB的React应用程序。当尝试从客户端发出post或get请求时,我们收到500状态码和以下错误: Proxy error: Could not proxy request /api/workdays/allworkdays/5e5ee6a690f9a13de897d6a6 from localhost:3000 to http://localhost:3001/. [1] See https://nodejs.org/api
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).
在post请求的情况下,信息被发布到数据库,尽管最终,我们确实会收到与w/a 500状态代码相同的错误消息
在get请求的情况下,请求在后端终端中得到控制,但浏览器从未接收到。“网络”选项卡反映(待定)指定,如下所示:
get请求的目的是查询我们的用户集合-检索所有链接的workday ID,然后查询Workdays集合并检索所有带有这些ID的文档
错误消息提供了指向错误解释(EconReset)的链接:
我们希望将这一反应作为事件推送到国家
我们的应用程序的工作流程是组件->Axios调用->API路由->控制器->数据库
任何关于这个错误的帮助将特别感谢,因为我是一个学生,希望很快被雇用。除了学习之外,我正在做这个项目,目的是把它包括在我的投资组合中
谢谢
我收到的错误:
我尝试过的事情:
代码:
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"
}
}
组件
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呼叫
**import axios from "axios";
export default {
// Gets all workdays w/ user id
getAllThisEmployeeWorkdays: function(id) {
return axios.get("/api/workdays/allworkdays/" + id);
}
};**
API路线
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
});
控制器
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.
模型
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
});
使用者
工作日
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);
因此,在检查代码12986次之后,我发现以下控制器文件的语法中有一个错误:
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));
}
在您通过id为用户查询数据库(并填充“workday”字段)后,.then不会获取文档,然后发送json响应(即res.json(document)
)
我建议将您的代码更改如下:
findAllById: function(req, res) {
db.User
.findById(req.params.id)
.populate("workday")
.then(document => {
res.json(document)
console.log("Returning all workdays", document);
return db.User.find(
{ workday: document },
);
})
}
希望这有帮助
findAllById: function(req, res) {
db.User
.findById(req.params.id)
.populate("workday")
.then(document => {
res.json(document)
console.log("Returning all workdays", document);
return db.User.find(
{ workday: document },
);
})
}