【问题标题】:ERR_CONNECTION_REFUSED in POST data to Mysql using NodeJs and ReactJs使用 NodeJs 和 ReactJs 将数据发布到 Mysql 中的 ERR_CONNECTION_REFUSED
【发布时间】:2020-06-03 09:31:51
【问题描述】:

我正在使用 ReactJs 和 NodeJS 将数据发送到 MySql,在我的 frontPage.js 中问题是我已经测试了使用 Postman 发布数据的 API,并且没有出现错误。当我尝试使用 ReactJs 时,问题就来了,我编写了这个代码:

class InputEndUser extends Component {
    constructor(props){
        super(props);

        this.state = {
            project_type: '',
            status : '',
            subject : '',
            enduser_name : '',
            location : '',
            cp_1 : '',
            cp1_hp : '',
            cp1_jpos: '',
            cp_2 : '',
            cp2_hp : '',
            cp2_jpos : '',
            quot_num : '',
            quot_date : '',
            note : ''

        }
        this.handleProject_type = this.handleProject_type.bind(this);
        this.handleStatus = this.handleStatus.bind(this);
        this.handleSubject = this.handleSubject.bind(this);
        this.handleEnduser_name = this.handleEnduser_name.bind(this);
        this.handleLocation = this.handleLocation.bind(this);
        this.handleCP1 = this.handleCP1.bind(this);
        this.handleCP1_hp = this.handleCP1_hp.bind(this);
        this.handleCP1_jpos = this.handleCP1_jpos.bind(this);
        this.handleCP2 = this.handleCP2.bind(this);
        this.handleCP2 = this.handleCP2.bind(this);
        this.handleCP2_hp = this.handleCP2_hp.bind(this);
        this.handleCP2_jpos = this.handleCP2_jpos.bind(this);
        this.handleQuot_num = this.handleQuot_num.bind(this);
        this.handleQuot_date = this.handleQuot_date.bind (this);
        this.handleNotes = this.handleNotes.bind(this);
    }

    handleProject_type(event){
        this.setState ({
            project_type : event.target.value
        })
    }

    handleStatus(event){
        this.setState({
            status: event.target.value
        })
    }

    handleSubject(event){
        this.setState({
            subject: event.target.value
        })
    }

    handleEnduser_name (event) {
        this.setState({
            enduser_name: event.target.value
        })
    }

    handleLocation(event){
        this.setState({
            location: event.target.value
        })
    }

    handleCP1(event){
        this.setState({
            cp_1: event.target.value
        })
    }

    handleCP1_hp(event){
        this.setState ({
            cp1_hp : event.target.value
        })
    }

    handleCP1_jpos(event){
        this.setState ({
            cp1_jpos : event.target.value
        })
    }

    handleCP2(event){
        this.setState({
            cp_2: event.target.value
        })
    }

    handleCP2_hp(event){
        this.setState ({
            cp2_hp : event.target.value
        })
    }

    handleCP2_jpos(event){
        this.setState ({
            cp2_jpos : event.target.value
        })
    }

    handleQuot_num(event) {
        this.setState ({
            quot_num : event.target.value
        })
    }

    handleQuot_date (event) {
        this.setState({
            quot_date : event.target.value
        })
    }

    handleNotes(event) {
        this.setState({
            note : event.target.value
        })
    }

    submitEndUser(event){
        event.preventDefault();
        fetch('http://localhost:4000/enduser', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        })
        .then ((response) => {
            return response.json;
        })
        .then ((body) => {
            console.log(body);
        })
        .catch((error) => {
            console.log(error)
        })
    }

和我在 server.js 中的 API:

app.post('/enduser', (req, res) => {
  let POST_ENDUSER_QUERY =  {
    project_id: req.body.project_id,
    project_type: req.body.project_type,
    status: req.body.status,
    subject: req.body.subject,
    endUser_name: req.body.endUser_name,
    location: req.body.location,
    cp_1: req.body.cp_1,
    cp1_hp: req.body.cp1_hp,
    cp1_jpos: req.body.cp1_jpos,
    cp_2: req.body.cp_1,
    cp2_hp: req.body.cp1_hp,
    cp2_jpos: req.body.cp1_jpos,
    quot_num: req.body.quot_num,
    quot_date: req.body.quot_date,
    note: req.body.note,
    image: req.body.image,
    estimated_value: req.body.estimated_value
  };
  if (!POST_ENDUSER_QUERY) {
    return res.status(400).send({ err: true, message: 'Please project name' });
  }

  let query = `INSERT INTO enduser_tbl (project_id, project_type, status, subject, endUser_name, location, cp_1, cp1_hp, cp1_jpos, cp_2, cp2_hp, cp2_jpos, quot_num,quot_date, note, image, estimated_value) VALUES (
    '${POST_ENDUSER_QUERY.project_id}',
    '${POST_ENDUSER_QUERY.project_type}',
    '${POST_ENDUSER_QUERY.status}',
    '${POST_ENDUSER_QUERY.subject}',
    '${POST_ENDUSER_QUERY.endUser_name}',
    '${POST_ENDUSER_QUERY.location}',
    '${POST_ENDUSER_QUERY.cp_1}',
    '${POST_ENDUSER_QUERY.cp1_hp}',
    '${POST_ENDUSER_QUERY.cp1_jpos}',
    '${POST_ENDUSER_QUERY.cp_2}',
    '${POST_ENDUSER_QUERY.cp2_hp}',
    '${POST_ENDUSER_QUERY.cp2_jpos}',
    '${POST_ENDUSER_QUERY.quot_num}',
    '${POST_ENDUSER_QUERY.quot_date}',
    '${POST_ENDUSER_QUERY.note}',
    '${POST_ENDUSER_QUERY.image}',
    '${POST_ENDUSER_QUERY.estimated_value}'
    )`;
  dbConn.query(query, ( err, results) => {
    if(err) throw err;
    return res.status(200).json({"status": 200, "err" : null, "data": results});
  });
});

然后错误来了:

当我用某种数据测试它时,也在终端中显示如下:

【问题讨论】:

  • 请阅读sqlMessage,你已经可以看到根本原因了。
  • 表示问题无关。你是对的!
  • @user207421 这个问题让我很困惑,我尝试使用邮递员测试 POST API,sql 和连接都没有错误。

标签: mysql node.js reactjs


【解决方案1】:

您没有在 React 的 POST 中添加正文数据:

fetch('http://localhost:4000/enduser', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        })

尝试将其更改为:

 fetch('http://localhost:4000/enduser', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            },
            body: JSON.stringify(this.state)
        })

【讨论】:

    【解决方案2】:

    您是否在 NodeJS API 中设置了 bodyparser ?我知道使用 express 和 koa 您必须安装一个模块来检索正文的内容并将其存储在 req.bodyctx.request.body 中。

    对于express

    对于koa

    【讨论】:

    • 我不好,我发现了问题,请看我的其他回复:stackoverflow.com/a/60296951/9681418
    • 这是错误localhost:4000/enduser net::ERR_CONNECTION_REFUSED。
    • 在API连接的情况下前端不要理会这个消息,先研究后端发现的错误,即@987654327的值@are undefined 看看我的其他答案,告诉我它是否有效。
    • 添加正文数据仍然不起作用,我在尝试获取数据时仍然出错,使用邮递员测试 API,我得到了这个 { "status": 200, "err": null, "数据”:{“fieldCount”:0,“affectedRows”:1,“insertId”:15,“serverStatus”:2,“warningCount”:0,“消息”:“”,“protocol41”:true,“changedRows” : 0 } }
    • 添加正文数据后,现在后端错误消息是什么?
    【解决方案3】:

    我的情况在这里遇到了问题... 在尝试了一些建议之后......

    我需要像这样在我的 POST 中包含正文:

     fetch('http://localhost:4000/enduser', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                },
                body: JSON.stringify(this.state)
            })
    

    最后我用mysql数据库中的Auto Increment字段解决了后端连接的问题。我用这个改变了 project_id 的值:

     let query = `INSERT INTO enduser_tbl (project_id, project_type, status, subject, endUser_name, location, cp_1, cp1_hp, cp1_jpos, cp_2, cp2_hp, cp2_jpos, quot_num,quot_date, note, image, estimated_value) VALUES (
    0,
    '${POST_ENDUSER_QUERY.project_type}',
    '${POST_ENDUSER_QUERY.status}',
    '${POST_ENDUSER_QUERY.subject}',
    '${POST_ENDUSER_QUERY.endUser_name}',
    '${POST_ENDUSER_QUERY.location}',
    '${POST_ENDUSER_QUERY.cp_1}',
    '${POST_ENDUSER_QUERY.cp1_hp}',
    '${POST_ENDUSER_QUERY.cp1_jpos}',
    '${POST_ENDUSER_QUERY.cp_2}',
    '${POST_ENDUSER_QUERY.cp2_hp}',
    '${POST_ENDUSER_QUERY.cp2_jpos}',
    '${POST_ENDUSER_QUERY.quot_num}',
    '${POST_ENDUSER_QUERY.quot_date}',
    '${POST_ENDUSER_QUERY.note}',
    '${POST_ENDUSER_QUERY.image}',
    '${POST_ENDUSER_QUERY.estimated_value}'
    )`;
    

    它的工作,我已经在 Postman 中测试过

    【讨论】:

      【解决方案4】:

      请设置 CORS 以使您的 Api 正常工作

      【讨论】:

      • 我已经在 server.js 中设置了 CORS ...我还需要在浏览器中设置它吗?
      • 你给它出处了吗?你能告诉我你控制台中的错误吗?
      • app.use(cors()); app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Credentials', 'true') ; res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE'); res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow -Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'); res.setHeader('Cache-Control', 'no-cache' ); 下一个(); });我已经把这一切都说完了......我认为这不是 CORS 出现的问题
      猜你喜欢
      • 2019-11-14
      • 2019-05-14
      • 2018-02-04
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多