Backend issue posting values with Sequelize to MySQL

57 views Asked by At

I have connected Sequelize to MySQL and the app successfully displays the data from my database in Postman and the browser. This is the initial data I inserted directly from the terminal into my database.

Now, I want to save new objects into my database through Sequelize but it only sends "null" values to the database as if it did not receive any values from my request.

However, when I post an object (User) from Postman, I do have a successful status and the body of my request has values of correct type.

Also added db.sequelize.sync() in my server.js file.

Any idea of what I could have missed?

Here is my code in the users.js controller file:

    const db = require('../models');
    const User = db.users;
    User.sync();

    exports.createUser = (req, res) => {

    const userWithPic = new User({
        ...req.body,
        picture: `${req.protocol}://${req.get('host')}/images/${req.file.filename}`,
    });

    User.create(userWithPic)
        .then((data) => res.send(data))
        .catch((err) => res.status(500).send(err);
        });
    };

Also noticed that the command npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string did create a model as it is supposed to however it did not create a migration in the migration folder. I doubt it is related however it should have.

Thanks !

1

There are 1 answers

0
Baulene On BEST ANSWER

I actually solved the issue. Just needed to remove the new User as Sequelize does not use this syntax with the create method.