我遇到了一个问题,我似乎无法透露我新创建的记录("组织"),从该记录中获取一个值(" organization.organizationId&#34 ;)然后在另一个表中更新记录的值(" user.organizationId")时应用该值。我看到update proccessing.aspects
set
aspects_name = :ASPECTS,
sentiments = :SENTIMENTS
where
id = :ID
和INSERT INTO
的SQL命令,但在UPDATE中找不到任何值。这对我来说没有意义,因为UPDATE
像这样揭示了这个对象:
console.log(organization);
以下是我的路线中的SQL命令:
INSERT INTO:
{ dataValues:
{ organizationId: 18,
organizationName: 'sdfsafnuffd',
admin: 'jdofsdjf@mfsdfa.com',
members: 'jdofsdjf@mfsdfa.com',
updatedAt: Mon Jan 11 2016 21:07:44 GMT-0500 (EST),
createdAt: Mon Jan 11 2016 21:07:44 GMT-0500 (EST) },
更新:
Executing (default): INSERT INTO `organization` (`organization_id`,`organization_name`,`admin`,`members`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'sdfsafnuffd','jdofsdjf@mfsdfa.com','jdofsdjf@mfsdfa.com','2016-01-12 02:07:44','2016-01-12 02:07:44');
organization.js模型:
Executing (default): UPDATE `user` SET `updatedAt`='2016-01-12 02:07:44' WHERE `user_id` = 18
user.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
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.hasMany(db.User, {foreignKey: 'user_id'});
},
},
});
return Organization;
}
db-index.js setup:
var bcrypt = require('bcrypt-nodejs');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('user', {
user_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: 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,
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
allowNull: true
}
}, {
freezeTableName: true,
classMethods: {
associate: function(db) {
User.belongsTo(db.Organization)
},
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
},
instanceMethods: {
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
},
},
});
return User;
}
路线:
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.Organization.associate(db);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
sequelize.sync();
module.exports = db;
答案 0 :(得分:0)
models.User.update({
annotationId: organization.organizationId
},{ where: { user_id: req.user.user_id }});
不确定这是否是类型o,但似乎没有在模型中定义annotationId。