有人可以帮我吗?我正在尝试在模型中创建带有验证的用户身份验证。我在我的node.js sequelize postgres项目中不断收到无法读取未定义属性'create'的错误。我使用邮递员时抛出了错误
model / index.js
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
// const config = require('../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-2) === '.js');
})
.forEach(file => {
const model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
model / user.js
const bcrypt = require('bcryptjs');
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
first_name: {
type: DataTypes.STRING,
allowNull: {
args: false,
msg: "Please provide a first name",
},
},
last_name: {
type: DataTypes.STRING,
allowNull: {
args: false,
msg: "Please provide a last name",
},
},
password: {
type: DataTypes.STRING,
allowNull: {
args: false,
msg: "Please provide a password",
},
validate: {
isNotShort: (value) => {
if (value.length < 8) {
throw new Error ('Password should be at least 8 characters')
}
}
}
},
email: {
type: DataTypes.STRING,
unique: {
args: true,
msg: "Email already exists",
},
allowNull: {
args: false,
msg: "Please provide an email address",
},
validate: {
isEmail: {
args: true,
msg: "Please enter a valid email address",
},
},
},
},
{}
);
User.beforeSave((user, options) => {
if (user.changed('password')) {
user.password = bcrypt.hashSync(
user.password,
bcrypt.genSaltSync(10),
null
);
};
});
User.prototype.comparePassword = (passw, cb) => {
bycrpt.compare(passw, this.password, (err, isMatch) => {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
User.associate = function(models) {
// associations can be defined here
};
return User;
};
controller / user.js
const express = require("express");
const jwt = require("jsonwebtoken");
const passport = require("passport");
const router = express.Router();
require("../config/passport")(passport);
const keys = require('../config/keys');
const User = require('../models').User;
const signup = (req, res) => {
if (!req.body.email || !req.body.password || !req.body.first_name || !req.body.last_name) {
res.status(400).send({ msg: "Please pass the required information." });
} else {
User
.create({
first_name: req.body.first_name,
last_name: req.body.last_name,
password: req.body.password,
email: req.body.username
})
.then((user) => res.status(201).send(user))
.catch((error) => {
console.log(error);
res.status(400).send(error);
});
}
};
module.exports = {
signup
}
router / user.js
const express = require("express");
const router = express.Router();
const usersController = require('../controllers/usersController');
router.post("/register", usersController.signup);
module.exports = router;
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
预先感谢您的帮助!