Faellor Faellor - 1 year ago 67
Node.js Question

Node.js Express with PostgresQL, error with sequelize

Sorry for the long and vague title.

I am trying to add a new user to my database with an http request. I have no problem creating the tables with my model files but for some reason I can't add a new user.

So here is the error I am getting:

Executing (default): SELECT 1+1 AS result
Connection to database successfull
TypeError: Cannot read property 'user' of undefined

here is my model file

export default function createUserModel (sequelize, DataTypes) {
const user = sequelize.define('user',{
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true
type: DataTypes.STRING,
allowNull: false,
unique: true
password: {
type: DataTypes.STRING,
allowNull: false
classMethods: {
associate (models){
user.belongsToMany(models.conversation, {through:'UserConversations'});

return user;

The controller (

var express = require('express');
var db = require('sequelize-connect');

var router = express.Router();

router.get('/', function (req, res, next) {
res.send('Hello World');

// Create new user
router.post('/', function (req, res, next) {

const createdUser = db.models.user.create({
email: req.body.email,
username: req.body.username,
password: req.body.password




module.exports = router;

My curl request:

curl -v -H "Content-type: application/json" -X POST http://localhost:3000/api/users/ -d '{"email":"test@test.com", "username":"test","password":"123456"}'

Answer Source

did you import your models to sequelize ? . There isn't user object in db.models