Answer the question
In order to leave comments, you need to log in
How to add data to database using sequelize?
the roures folder contains the auth file with the code
const express = require('express'),
router = express.Router(),
bcrypt = require('bcrypt-nodejs'),
models = require('../models');
router.post('/register', (request, response)=>{
const login = request.body.name,
mail = request.body.email,
pass = request.body.password,
repass = request.body.repassword;
if(!login || !mail || !pass || !repass){
response.json({
ok: false,
error: 'Все поля должны быть заполнены!',
fields: ['login', 'mail', 'pass', 'repass']
});
}else if(login.length < 3 || login.length > 16){
response.json({
ok: false,
error: 'Длина логина от 3 до 16 символов!',
fields: ['login']
});
}else if(pass !== repass){
response.json({
ok: false,
error: 'Не совпадают пароль',
fields: ['pass', 'repass']
})
}else{
bcrypt.hash(pass, null, null, function(err, hash){
models.User.create({
login: login,
mail: mail,
password: hash,
privilege: 1
}).then(user => {
console.log(user);
response.json({
ok:true
})
}).catch(err => {
console.log(err);
})
})
}
});
module.exports = router;
models.User.create({
login: login,
mail: mail,
password: hash,
privilege: 1
})
models.User.create({
TypeError: models.User.create is not a function
const Book = require('./book'),
User = require('./user');
module.exports = {
Book,
User
}
const database = require('../database'),
User = database.sequelize.define('user',{
id: {
type: database.Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
login: {
type: database.Sequelize.STRING,
allowNull: false,
unique: true
},
password: {
type: database.Sequelize.STRING,
allowNull: false
},
mail: {
type: database.Sequelize.STRING,
allowNull: false,
unique: true
},
privilege:{
type: database.Sequelize.INTEGER,
allowNull: false
}
});
module.exports = {
User: User
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question