1

I faced this issue where it gave me Cannot GET /register, but why get for the front end is working and not register, do I'm missing something here ?

error in fetch in registerjs file

log picture error in fetch pic

const handleRegister = (req,res,db,bcrypt) => {
    const {email,name,password} = req.body;
    if (!email || !name || !password){
      return res.status(400).json('incorrect form submission');
    }
    const saltRounds = 10;
    const hash = bcrypt.hashSync(password,saltRounds);
    const salt = bcrypt.genSaltSync(saltRounds);
    db.transaction(trx => {
        trx.insert({
            hash : hash,
            email : email
        })
        .into ('login')
        .returning('email')
        .then(loginEmail => {
            return trx('users1')
            .returning('*')
            .insert ({
                email : loginEmail[0],
                name : name,
                joined : new Date()
            })
            .then (user => {
                res.json(user[0]);
            })
        })
        .then(trx.commit)
        .catch(trx.rollback)
    })

    .catch(err => res.status(400).json('unable to register'))
}

module.exports = {
    handleRegister : handleRegister
};
RangerRanger
  • 2,455
  • 2
  • 16
  • 36

2 Answers2

0

@VyasArpit

route file

import React from 'react';

class Register extends React.Component  {
    constructor (props) {
        super(props);
        this.state = {
            email : '',
            password : '',
            name : ''
        }
    }
    onNameChange = (event) => {
        this.setState({name : event.target.value})
    }
    onEmailChange = (event) => {
        this.setState({email : event.target.value})
    }
    onPasswordChange = (event) => {
        this.setState({password : event.target.value})
    }
    onSubmitSignIn = () => {
        fetch(' https://dry-mesa-91343.herokuapp.com/register', {
            method : 'post',
            headers : {'Content-Type' : 'application/json'},
            body : JSON.stringify ({
                email : this.state.email,
                password : this.state.password,
                name : this.state.name
            })  
        })
            .then(response => response.json())
            .then(user => {
                if (user.id){
                    this.props.loadUser(user);
                    this.props.onRouteChange('home');
                }
            })
    }
    render () {
        return (
            <article className ="br3 ba dark-gray b--black-10 mv4 w-120 w-50-m w-25-l mw5  shadow-5 center">
                    <main className ="pa4 black-80">
                    <div className ="measure">
                    <fieldset id="sign_up" className ="ba b--transparent ph0 mh0">
                    <legend className ="f2 fw6 ph0 mh0">Register</legend>
                    <div className ="mt3">
                        <label className ="db fw6 lh-copy f6" htmlFor="name">Name</label>
                        <input 
                        className ="pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100"
                         type="text" 
                         name="name" 
                          id="name"
                          onChange = {this.onNameChange}
                          />
                    </div>
                    <div className ="mt3">
                        <label className ="db fw6 lh-copy f6" htmlFor="email-address">Email</label>
                        <input 
                        className ="pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" 
                        type="email"
                         name="email-address"  
                         id="email-address"
                         onChange = {this.onEmailChange}  
                         />
                    </div>
                    <div className ="mv3">
                        <label className ="db fw6 lh-copy f6" htmlFor="password">Password</label>
                        <input 
                        className ="b pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" 
                        type="password" 
                        name="password" 
                         id="password"
                         onChange = {this.onPasswordChange}
                         />
                    </div>
                    </fieldset>
                    <div className ="">
                        <input 
                            onClick= {this.onSubmitSignIn} 
                            className ="b ph3 pv2 input-reset ba b--black bg-transparent grow pointer f6 dib"
                             type="submit" 
                             value="Register"/>
                    </div>
                </div>
             </main>
            </article>
        );
    }
}

export default Register;
0

In your server.js file, you should include the following code in order to connect your database url to the backend :

const db= knex({
  client: 'pg',
  connection: {
    connectionString : process.env.DATABASE_URL,
    ssl: false,
});

I have the same issue - I could not register new users untill I changed the ssl to false (you can watch this I can not connect to Postgres DB with Strapi on Heroku).

From the official tutorial of heroku : enter image description here

In my case, I have just wrote:

ssl: false

If it didnt worked, try read that: TypeError: Cannot call method 'query' of null - when calling pg.connect with Heroku node.js

Nathan Barel
  • 348
  • 2
  • 14