0

I have added my backend to heroku and my frontend to netlify, however when I'm trying to run my app whether it is locally or via netlify I'm getting the below error message when logging in or/and registering:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

what am I missing?

see below my app.js and signin.js for the frontend: here is my APP.JS

import React, {Component} from 'react';
// import Clarifai from 'clarifai';
import Navigation from './components/Navigation/Navigation';
import Logo from './components/Logo/Logo';
import ImageLinkForm from './components/ImageLinkForm/ImageLinkForm';
import FaceRecognition from './components/FaceRecognition/FaceRecognition';
import Rank from './components/Rank/Rank';
import Signin from './components/Signin/Signin';
import Register from './components/Register/Register';
import './App.css';







const intialState = {
   
      input: '',
      imageUrl: '',
      box: {},
      route: 'signin',
      isSignedIn: false,
      user: {
        id: '',
        name: '',
        email: '',
        entries: 0,
        joined: ''
      }
    }


class App extends Component {
  constructor() {
    super();
    this.state = intialState
  }

  loadUser = (data) => {
    this.setState({user: {
      id: data.id,
      name: data.name,
      email: data.email,
      entries: data.entries,
      joined: data.joined
    }})
  }


  calculateFaceLocation =(data) => {
   const clarifaiFace = data.outputs[0].data.regions[0].region_info.bounding_box;
   const image = document.getElementById('inputimage');
   const width = Number(image.width);
   const height = Number(image.height);
   return {
    leftCol: clarifaiFace.left_col * width,
    topRow: clarifaiFace.top_row * height,
    rightCol: width - clarifaiFace.right_col * width,
    bottomRow: height - clarifaiFace.bottom_row * height,
  };
}

displayFaceBox = (box) => {
  console.log(box)
  this.setState({box: box});
}

onInputChange = (event) => {
  this.setState({input: event.target.value})
}

onButtonSubmit = () => {
  this.setState({imageUrl: this.state.input})
    fetch('https://ancient-sea-46547.herokuapp.com/imageurl', {
        method: 'post',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
          input: this.state.input
        })
      })
 
  .then(response => response.json())
  .then( response => {
    if (response) {
      fetch('https://ancient-sea-46547.herokuapp.com/image', {
        method: 'put',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
          id: this.state.user.id
        })
      })
      .then(response => response.json())
      .then(count => {
        this.setState(Object.assign(this.state.user, { entries: count}))
      })
      .catch(console.log)
    }
    this.displayFaceBox(this.calculateFaceLocation(response))
  })
  .catch(err => console.log(err));

}

onRouteChange = (route) => {
  if (route === 'signout') {
    this.setState(intialState)
  } else if (route === 'home') {
    this.setState({isSignedIn: true})
  }
  this.setState({route:route});
}




render() {
  return (
    <div className="App">
     

    <Navigation isSignedIn={this.state.isSignedIn} onRouteChange={this.onRouteChange} />
    { this.state.route === 'home' 
    ? <div>
    <Logo />
    <Rank 
    name={this.state.user.name} 
    entries={this.state.user.entries}/>
    <ImageLinkForm  
    onInputChange={this.onInputChange}
    onButtonSubmit={this.onButtonSubmit} />
    <FaceRecognition box={this.state.box} imageUrl={this.state.imageUrl}/>
    </div>
    :(
      this.state.route === 'signin'
      ?<Signin loadUser={this.loadUser} onRouteChange={this.onRouteChange} />
      :<Register loadUser={this.loadUser} onRouteChange={this.onRouteChange} />
      )


    
  }
  </div>
  );
}
}

export default App;

SIGNIN.JS

import React from 'react';

class Signin extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      signInEmail: '',
      signInPassword: ''
    }
  }
  onEmailChange = (event) => {
    this.setState({signInEmail: event.target.value})
  }

  onPasswordChange = (event) => {
    this.setState({signInPassword: event.target.value})
  }



  onSubmitSignIn = () => {
    fetch('https://ancient-sea-46547.herokuapp.com/signin', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        email: this.state.signInEmail,
        password: this.state.signInPassword
      })
    })
    .then(response => response.json())
    .then(user => {
      if (user.id) {
        this.props.loadUser(user);
        this.props.onRouteChange('home');
      }
    })
    
  }

  render() {
    const { onRouteChange} = this.props;
    return (
        <article className="br3 ba  b--black-10 mv4 w-100 w-50-m w-25-l mw6 shadow-5 center">
         <main className="pa4 black-80">
          <div className="measure">
            <fieldset id="sign_up" className="ba b--transparent ph0 mh0">
             <legend className="f1 fw6 ph0 mh0">Sign In</legend>
           <div className="mt3">
            <label className="db fw6 lh-copy f6" for="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" for="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="Sign in" />
        </div>
        <div className="lh-copy mt3">
          <p onClick={() => onRouteChange('register')}  className="f6 link dim black db pointer">Register</p>
           
         </div>
       </div>
     </main>
  
  </article>
  );
  }

    
        
}

export default Signin;

here is my server.js

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '1';



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



const app = express();



app.use(bodyParser.json())
app.use(cors())

app.get('/', (req, res) => {res.send('it is working') })
app.post('/signin',  (req, res) => {signin.handleSignin(req, res, db, bcrypt)})
app.post('/register',  (req, res) => {register.handleRegister(req, res, db, bcrypt)})
app.get('/profile/:id', (req, res) => {profile.handleProfile(req, res, db)})
app.put('/image', (req, res) => {image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => {image.handleApiCall(req, res)})





app.listen(process.env.PORT || 3001, () => {
    console.log(`app is running on port ${process.env.PORT}`)
})

register.js

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://ancient-sea-46547.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  b--black-10 mv4 w-100 w-50-m w-25-l mw6 shadow-5 center">
         <main className="pa4 black-80">
          <div className="measure">
            <fieldset id="sign_up" className="ba b--transparent ph0 mh0">
             <legend className="f1 fw6 ph0 mh0">Register</legend>
             <div className="mt3">
            <label className="db fw6 lh-copy f6" for="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" for="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" for="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 className="lh-copy mt3">
         </div>
       </div>
     </main>
  </article>
Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
Morris
  • 13
  • 4
  • Can we se the Rank and Register Components too? – Colin Hale Sep 08 '22 at 15:43
  • Rank.js import React from 'react'; const Rank = ({ name, entries }) => { return(
    {`${name}, your current entry count is...`}
    {entries}
    ); } export default Rank;
    – Morris Sep 08 '22 at 15:44
  • i have added the register.js to my question above, however, i didn't change anything except for putting my frontend on netlify and backend on heroku, and also added the herkou url to my frontend project – Morris Sep 08 '22 at 15:47
  • Whats is name and entries in your Rank.js. can you confirm those are both strings? What this error is saying is that React is trying to render something to the DOM that should be a component or string but is an object instead. Usually this happens when you think something like {name} is a string but ends up being an object. – Colin Hale Sep 08 '22 at 16:18
  • yes those are both strings. it just started happening as soon i put my frontend on netlify – Morris Sep 08 '22 at 16:32
  • `register.js` doesn't export anything. FWIW: this is a giant amount of code to sift through in order to find what component isn't rendering. If it doesn't work locally then netlify doesn't matter. Easiest approach is to check your assumptions (e.g., are imports what you expect after importing), then bisecting code to see what breaks everything. This assumes there's no more additional information in the log, like what component introduces the bad render. – Dave Newton Sep 08 '22 at 18:16
  • there is really nothing i can see in the register.js or any other components, it has been working fine until i put on Netlify, i had deleted my node-moduels and reinstalled it, this is my website https://detectfaces.netlify.app/ could something is off with my github repo? https://github.com/Moshe844/frontend-project also, when running it local the sign in and registser for my app is centered and when running from netlify it's tilted to the left – Morris Sep 08 '22 at 19:36

0 Answers0