0

I am trying to connect my frontend to my backend, I keep getting a internal 500 error when trying to connect my signin response to the backend sequelize postgres servers.

Here are the errors:

notNull Violation: User.username cannot be null,
notNull Violation: User.email cannot be null,
notNull Violation: User.password cannot be null
    at InstanceValidator._validate (C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\node_modules\sequelize\lib\instance-validator.js:50:13)
    at async InstanceValidator._validateAndRunHooks (C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\node_modules\sequelize\lib\instance-validator.js:60:7)
    at async InstanceValidator.validate (C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\node_modules\sequelize\lib\instance-validator.js:54:12)
    at async User.save (C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\node_modules\sequelize\lib\model.js:2360:7)
    at async Function.create (C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\node_modules\sequelize\lib\model.js:1336:12)
    at async C:\Users\donal\videos\Web-Developement\videojs\yt-clone\yt-backend2\routes\auth.js:10:22 {
  errors: [
    ValidationErrorItem {
      message: 'User.username cannot be null',
      type: 'notNull Violation',
      path: 'username',
      value: null,
      origin: 'CORE',
      instance: [User],
      validatorKey: 'is_null',
      validatorName: null,
      validatorArgs: []
    },
    ValidationErrorItem {
      message: 'User.email cannot be null',
      type: 'notNull Violation',
      path: 'email',
      value: null,
      origin: 'CORE',
      instance: [User],
      validatorKey: 'is_null',
      validatorName: null,
      validatorArgs: []
    },
    ValidationErrorItem {
      message: 'User.password cannot be null',
      type: 'notNull Violation',
      path: 'password',
      value: null,
      origin: 'CORE',
      instance: [User],
      validatorKey: 'is_null',
      validatorName: null,
      validatorArgs: []

Console Error from Inspect:

POST http://localhost:5000/api/auth/register 500 (Internal Server Error)

Here is my code: api.js

import axios from 'axios';

const apiClient = axios.create({
  baseURL: "http://localhost:5000/api",
  timeout: 1000,
});

export const authenticate = async (data) => {
  try {
    return await apiClient.post("/auth/register", data);
  } catch (exception) {
    return {
      error: true,
      exception,
    };
  }
};

auth from routes:

router.post('/register', async(req, res) => {
    const { username, email, password } = req.body;

    try {
        const user = await User.create({ username, email, password });

        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(user.password, salt);
    
        return res.json(user)
      } catch (err) {
        console.log(err)
        return res.status(500).json(err)
      };
});

My response body is coming from the signin component itself from my React frontend, not Postman. I think an issue it could be is that the server is not reconginizing it as json and still seeing it as html.

Donnyb
  • 21
  • 8
  • did you check `req.body`? Is it ab object with all needed fields? Did you use JSON parser in express? – Anatoly Apr 26 '22 at 18:09
  • I'm using *app.use(express.json());* in my server page for express. I thought that axios turned all requests into Json, I'm not sure if it's needed, but I could upload the useInput and reducer information if needed. – Donnyb Apr 26 '22 at 18:57
  • Yes, axios sends all requests by default as JSON – Anatoly Apr 26 '22 at 19:31
  • So what should be in output after `console.log(req.body)`? – Anatoly Apr 26 '22 at 19:32
  • I don't have **console.log(req.body)** in my code. are you suggesting I put in **res.json(req.body)**? – Donnyb Apr 26 '22 at 19:38
  • Simply add it right after `const { username, email, password } = req.body; ` – Anatoly Apr 26 '22 at 19:50
  • I didn't get after entering console.log(req.body), I did get two different errors from the front and backend. "Unexpected token " in JSON at position 0" and "Access to XMLHttpRequest at 'http://localhost:5000/api/auth/register' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." – Donnyb Apr 26 '22 at 20:01
  • How does `data` look like? Can you show the request headers and body? – Anatoly Apr 26 '22 at 20:17
  • 1) You are receiving the string (" at pos 0) where you should receive the JSON => Try enforcing the data type by adding `Content-Type` on `axios` side. https://masteringjs.io/tutorials/axios/post-json#:~:text=If%20you%20pass%20a%20JavaScript,Express%20can%20automatically%20parse%20it. 2) You have CORs disabled. => set this up in express. https://stackoverflow.com/a/40026625/2956135 and let's see if these will solve issues. – Emma Apr 27 '22 at 14:59

0 Answers0