javascript - JSON解析错误: unrecognised token <

标签 javascript json node.js express react-native

我正在尝试在我的应用程序中实现用户注册。我使用 Node.js Express 创建后端,并在我在路由器文件夹中创建的 .js 文件中传递插入查询。我也在 app.js 中添加了所有必要的东西。

以下是我的后端代码。

var express = require('express');
var router = express.Router();
var mysql =  require('mysql');

var connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database:'MobileDB',
})

/* GET users listing. */
router.post('/', function(req, res, next) {

        var name = req.body.name;
        var email = req.body.username;
        var password = req.body.password;
        var phone = req.body.phone;
        connection.query("INSERT INTO `user` (`id`, `name`, `username`, `password`, `phone`) VALUES (NULL, '', '', '', '')",[null,name,username,password,phone],function(err,row,fields){
          if (err) console.log(err);

          if(row.length > 0){
              res.send({'success': true, 'message': 'welcome new user'});
          } else {
            res.send({'success':false,'message':'user not found, please try again'});
          }
      });
  });

module.exports = router;

以下是我的注册页面:

import React, { Component } from 'react';
import{
  Image,
  View
} from 'react-native';
import { Container, Content, List,Thumbnail, ListItem, InputGroup, Input, Icon, Text, Picker, Button,Header,Footer} from 'native-base';
import {
Router,
Actions,
Scene } from 'react-native-router-flux';
const Item = Picker.Item;
export default class Reg extends Component {
    constructor(props) {
        super(props);
        this.state = {
            name:'',
            username:'',
            password:'',
            phone:'',
            }

        };
    }
      Reg = () => {


            fetch('http://192.168.0.20:3000/reg', {
             method : 'POST',
                  headers: {
                  'Accept': 'application/json',
                  'Content-type': 'application/json',
                  },

                  body: JSON.stringify({
                  name:this.state.name,
                  username:this.state.username,
                  password:this.state.password,
                  phone:this.state.phone,

                  })
            })

            .then((response) => response.json())
            .then((res) => {
                  if(res.success=== true){
                  var username = res.message;

                     AsyncStorage.setItem('username', username);
                     Actions.Ideas();}
                     else {
                    alert(res.message);
                }



            })

            .done();

      }
    render(){
      return(
      <Container>
            <Content>
                    <View style={{alignSelf:'center'}}>
                    <Button transparent onPress={Actions.upload}>
                    <Icon name='ios-contact' style={{fontSize:30}}/>
                    </Button>
                    </View>
                    <View style={{top:20}}>
                    <List>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-person-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(name) => this.setState({name})}
                                  value={this.state.name} placeholder="Name" />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-at-outline" style={{color:'#5bc0de'}}/>
                                <Input onChangeText={(username) => this.setState({username})}
                                   value={this.state.username} placeholder="Email" />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-lock-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(password) => this.setState({password})}
                                 value={this.state.password}placeholder="Password" secureTextEntry />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-call-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(phone) => this.setState({phone})}
                                 value={this.state.phone}placeholder="Phone" keyboardType="numeric" />
                            </InputGroup>
                        </ListItem>

                    </List>
                    <Button info style={{ alignSelf: 'center', marginTop:20, marginBottom: 20 }} onPress={this.Reg}>
                        SIGN UP
                    </Button>
                    </View>
                </Content>
            </Container>

        );
    }
}

当我单击“注册”按钮时,它显示无法识别的 token 错误。我不知道我哪里错了。请帮忙。

最佳答案

附加 Chrome 调试器并启用异常暂停,您将了解出了什么问题。

关于javascript - JSON解析错误: unrecognised token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43012986/

相关文章:

javascript - 带有 webpack 的 UglifyJs 在进行生产构建时会卡住

javascript - 我的 jsconfig.json 在 react js 中有一个错误说 throw err Unexpected token } in JSON at position

javascript - .htaccess 文件无法在我的 Node.js 应用程序上运行

javascript - 使用 promise 时,为什么最后一个被调用?

javascript - 当不支持 JavaScript 时,我可以使用 Node.js 'mirror' 作为回退吗?

Javascript cookie变量问题

javascript - Dojo dgrid 更新一行

javascript - VueJS 中的数据绑定(bind)以构建 URL

java - 如何使用 Jackson 将对象附加到现有 JSON 文件

python - 如何合并 JSON 数组和数组