Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Viewed 15636

I try to create signup page and send data to mysql db. I just wrote this codes.. server.js

  var mysql = require('mysql');
    var http = require('http');
    var port = process.env.PORT || 1337;




    var con = mysql.createConnection({
        host: "localhost",
        user: "*****",
        password: "*******",
        database: "node"
    });

    con.connect(function (error) {
        app.post('/', function (req, res) {

            var user = req.body;

            var query = connection.query('INSERT INTO signup(FirstName, LastName, Email,Password, PasswordConfirm) SET ?', user, function (err, result) {

            });
            res.end('Success');
        });
    });

app.js this is my component.

class Signup extends React.Component {


    constructor() {
        super();
        this.state = { user: {} };
        this.onSubmit = this.handleSubmit.bind(this);
    }
    handleSubmit(e) {
        e.preventDefault();
        var self = this;

        fetch('/', { 
            method: 'POST',
            data: {
                firstname: self.refs.FirstName,
                lastname: self.refs.LasttName,
                email: self.refs.Email,
                password: self.refs.Password,
                passwordconfirm: self.refs.PasswordConfirm

            }
        })
          .then(function(response) {
              return response.json()
          }).then(function(body) {
              console.log(body);
          });
    }


    render(){
        return(
            <div>


        <form id="signup" onSubmit={this.onSubmit}>
            <input type="text" id="firstName" placeholder="FirstName" ref="firstname"/>
            <input type="text" id="lastName"  placeholder="LastName" ref="lastname"/>
            <input type="email" id="email" placeholder="Email" ref="email"/>    
            <input type="password" id="password" placeholder="Password" ref="password" />
            <input type="password" id="confirm" placeholder="PasswordConfirm"ref="passwordconfirm" />
            <input type="submit" />

        </form>
        </div>

             )
    }}

I just try to send data to mysql db. I got this error message -->Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Why?What is wrong?

3 Answers

Ok I encounter this problem just by forgetting the proxy in my package.json. Be sure to check it.

Related