0

I am trying to implement user registration in my app. I used node.js express to create my backend and passed a insert query in a .js file i created in the router folder. I have added all the necessary things in app.js too.

Following is my backend code.

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;

and The following is my registration page:

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>

        );
    }
}

When i click on the SignUp button it shows unrecognised token error. I can't figure out where i went wrong. Please help.

8
  • print 'response' from promise Commented Mar 25, 2017 at 6:01
  • i don't understand. Commented Mar 25, 2017 at 6:02
  • 192.168.0.20:3000 this IP is your local machine IP or globe IP ? Commented Mar 25, 2017 at 6:10
  • @ArunkumarRamaraj local machine IP Commented Mar 25, 2017 at 6:14
  • It worked fine for login. But i don't know why it isn't working for registration. Commented Mar 25, 2017 at 6:14

2 Answers 2

1

Attach Chrome debugger and enable pause on exceptions, you will get to know what is going wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

Inside your Reg() function, 'http://192.168.0.20:3000/reg' should be 'http://192.168.0.20:3000/' I believe.

I think you're getting back some unwanted HTML with your current code. I assume its because you're attempting to send a POST request to an undefined route.

Right before your if statement, console.log the response res so you know exactly what you're getting back from your backend.

10 Comments

and i am not getting any thing. when console.log. It just goes straight to the error screen.
This might not make a difference but capitalize the T in Content-Type in the headers section of the request.
Also your route is defined as / not /reg
Do you know how to debug remotely using chrome tools?
what are you using to run the app? xcode? android studios?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.