Sequelize returns error during api test

Asked

Viewed 13 times

-2

Good afternoon friends!

I created a Rest api based on a youtube video using Node, express and sequelize. When sending an empty post, for one of the routes without access to the bank, I get return normally.

But when sending a post to create database entries, sequelize uses my PC user credentials, not the user data I entered into the code.

gives me the error: Access denied for user 'degrossoli'@'localhost'

  • Route: http://localhost:3030/create
  • Json content:
{
  "username": "marcos",
  "password": 123
}

On the controller I have

async create(req, res) {
        const response = { ...responseModel };

        const { username, password } = req.body;

        const [, affectRows] = await connection.query(`
            INSERT INTO users VALUES (
                DEFAULT,
                '${username}',
                '${password}',
                NOW(),
                NOW()
            );
        `)

        response.success = affectRows > 0;

        return res.json(response);
    },

My Connection

const Sequelize = require('sequelize');

const database = process.env.DATABASE;
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
const host = process.env.HOST;
const dialect = "mysql";

const connection = new Sequelize(database, username, password, {
    host,
    dialect
})

module.exports = connection;

All content is in: https://github.com/Marcosed1979/api-com-express

Thank you very much!

print do erro

1 answer

0

RESOLVED

In the connection instance were variables for the database access data. I replaced the variables with the access data and was able to write to the database successfully.

Stayed like this:

const Sequelize = require('sequelize');

const database = process.env.DATABASE;
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
const host = process.env.HOST;
const dialect = "mysql";

const connection = new Sequelize('bancodeteste', 'developer', 'd3v3l0p3r', {
   host,
   dialect
})

module.exports = connection;

Now I need to understand how to hide my access data. But I’m already happy. First API working!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.