如何在Sequelize.js ORM中正确创建新条目?

时间:2019-01-21 10:44:13

标签: node.js express orm

我在Node.js应用程序中使用 Sequelize.js ORM。

当我尝试通过/create网址创建新用户时,会引发错误。

错误消息:TypeError: User.create is not a function

我在哪里弄错了?

models / user.js

'use strict';

const bcrypt = require('bcrypt-nodejs');

module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define('User', {
        email: DataTypes.STRING,
        password: DataTypes.STRING,
        first_name: DataTypes.STRING,
        end_name: DataTypes.STRING,
        active: DataTypes.BOOLEAN,
        admin: DataTypes.BOOLEAN
    }, {
        timestamps: false,
        createdAt: false,
        updatedAt: false,
    });
    User.beforeSave((user) => {
        if (user.changed('password')) {
            user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
        }
    });
    User.prototype.comparePassword = function (password, cb) {
        bcrypt.compare(password, this.password, function (err, isMatch) {
            if (err) {
                return cb(err);
            }
            cb(null, isMatch);
        });
    };
    return User;
};

routes / user.js:

const express = require('express');
const router = express.Router();

const User = require('../models/user');

router.post('/create', function(req, res) {
    console.log(req.body);
    if (!req.body.email || !req.body.password) {
        res.status(400).send({
            msg: 'Please enter email and password.'
        });
    } else {
        User.create({
            email: req.body.email,
            password: req.body.password,
            first_name: req.body.first_name,
            last_name: req.body.last_name,
            active: req.body.active,
            admin: req.body.admin
        }).then((user) => res.status(201).send(user)).catch((error) => {
            console.log(error);
            res.status(400).send(error);
        });
    }
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。

configurations.js / PostgreSQL.js:

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'user', 'password', {
    host: 'host',
    dialect: 'postgres'
});

module.exports = sequelize;

models / users.js:

const sequelize = require('../configurations/PostgreSQL');
const bcrypt = require('bcrypt-nodejs');

const users = sequelize.define('users', {
    email: sequelize.Sequelize.STRING,
    password: sequelize.Sequelize.STRING,
    first_name: sequelize.Sequelize.STRING,
    last_name: sequelize.Sequelize.STRING,
    active: sequelize.Sequelize.BOOLEAN,
    admin: sequelize.Sequelize.BOOLEAN
}, {
    timestamps: false,
    createdAt: false,
    updatedAt: false,
});

users.beforeSave((user) => {
    if (user.changed('password')) {
        user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
    }
});

users.prototype.comparePassword = function (password, cb) {
    bcrypt.compare(password, this.password, function (err, isMatch) {
        if (err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
};

module.exports = users;

routes / users.js:

const express = require('express');
const router = express.Router();

const passport = require('passport');
require('../configurations/password')(passport);

const users = require('../models/users');

router.post('/create', function(req, res) {
    if (!req.body.email || !req.body.password) {
        res.status(400).send({
            status: false,
            message: ''
        });
    } else {
        users.create({
            email: req.body.email,
            password: req.body.password,
            first_name: req.body.first_name,
            last_name: req.body.last_name,
            active: req.body.active,
            admin: req.body.admin
        }).then((user) => res.status(201).send(user)).catch((error) => {
            console.log(error);
            res.status(400).send(error);
        });
    }
});

module.exports = router;