gpt4 book ai didi

node.js - Sequelize Tables 不相关的问题

转载 作者:行者123 更新时间:2023-12-03 22:42:23 26 4
gpt4 key购买 nike

我遇到了一个问题,我试图在一个路由 POST 方法中为多个表填充一个值。我阅读了文档中有关创建关联的部分并模仿了该示例,但我遇到了一个问题:

Error: organization is not associated to user!
at validateIncludedElement (/Users/user/Desktop/Projects/node/app/node_modules/sequelize/lib/model.js:560:11)

我不确定为什么会发生这种情况,因为我使用的是应该创建关联的 .belongsTo 方法。

这是 User.js 模型:
var bcrypt   = require('bcrypt-nodejs');

module.exports = function(sequelize, DataTypes) {

var User = sequelize.define('user', {
user_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
organizationName: {
type: DataTypes.STRING,
field: 'organization_name',
unique: true
},
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
},
email: {
type: DataTypes.STRING,
isEmail: true,
unique: true
},
password: DataTypes.STRING,

}, {
freezeTableName: true
},{
classMethods: {
associate: function(db) {
User.belongsTo(db.Organization)
}
}
});
return User;
}

这是organization.js 模型:
module.exports = function(sequelize, DataTypes) {

var Organization = sequelize.define('organization', {
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
autoIncrement: true,
primaryKey: true
},
organizationName: {
type: DataTypes.STRING,
field: 'organization_name'
},
admin: DataTypes.STRING,
members: DataTypes.STRING
});

return Organization;
}

这是模型索引文件,其中表附加到 db 对象 db-index.js:
var Sequelize = require('sequelize');
var path = require('path');
var config = require(path.resolve(__dirname, '..', '..','./config/config.js'));
var sequelize = new Sequelize(config.database, config.username, config.password, {
host:'localhost',
port:'3306',
dialect: 'mysql'
});

sequelize.authenticate().then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});

var db = {}

db.Organization = sequelize.import(__dirname + "/organization");

db.User = sequelize.import(__dirname + "/user");

db.Annotation = sequelize.import(__dirname + "/annotation");


db.Annotation.associate(db);

db.sequelize = sequelize;
db.Sequelize = Sequelize;

sequelize.sync();

module.exports = db;

以下是路线( /sign-up 是我引用的地方):
var express = require('express');
var siteRoutes = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');

/*==== Passport Configuration ====*/

// Serialize sessions
passport.serializeUser(function(user, done) {
console.log(user.user_id + "Serializing");
done(null, user.user_id);
});

passport.deserializeUser(function(user_id, done) {
models.User.find({where: {user_id: user_id}}).then(function(user){
done(null, user);
}).error(function(err){
done(err, null);
});
});


passport.use('local', new LocalStrategy({
passReqToCallback : true,
usernameField: 'email'
},
function(req, email, password, done) {
//Find user by email
models.User.findOne({
where: {
email: req.body.email,
password: req.body.password
}
}).then(function(user) {
return done(null, user);
})
.catch(function(err) {
return done(null, false, req.flash('message', 'Email not found.'));
});
}
));




/*==== Index ====*/

siteRoutes.get('/', function(req, res){
res.render('pages/index.hbs');
});

/*==== Login ====*/


siteRoutes.route('/login')

.get(function(req, res){
res.render('pages/login.hbs');
})

.post(passport.authenticate('local', {
successRedirect: '/app',
failureRedirect: '/login',
failureFlash: 'Invalid username or password.'
}));



siteRoutes.route('/sign-up')

.get(function(req, res){
res.render('pages/sign-up.hbs');
})

.post(function(req, res){

models.User.create({
Organization: {
organizationName: req.body.organizationName
},
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password
},{include: [models.Organization]}).then(function() {
res.redirect('/');
}).catch(function(error){
res.send(error);
})

});







module.exports = siteRoutes;

最佳答案

你只关联模型注释

db.Annotation.associate(db);

忘记
db.User.associate(db);

关于node.js - Sequelize Tables 不相关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34475024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com