gpt4 book ai didi

node.js - Sequelize 不同步 - 插入

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:16 25 4
gpt4 key购买 nike

这是我的代码:

models/index.js:

var Sequelize = require('sequelize');
// initialize database connection
var sequelize = new Sequelize('somedb', '', '', {
host: 'localhost',
dialect: 'sqlite',
pool:{
max: 5,
min: 0,
idle: 10000
},
storage: "db/main.db"
});

// load models
var models = [
'Users',"Clients"
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
module.exports[model].sync();
});

// export connection
module.exports.sequelize = sequelize;

app.js:

SQLized.sync().then(function(err){
//Settings Environmental Variables
var env = process.env.STAGE;
var config = require("./config.js")[env];
...
});

models/Users.js:

var Sequelize = require("sequelize");
var hash = require("../modules/util/hash");

module.exports=function(sequelize, DataTypes){
return Users = sequelize.define("Users", {
id: {
type: DataTypes.INTEGER,
field: "id",
autoIncrement: !0,
primaryKey: !0
},
firstName: {
type: DataTypes.STRING,
field: "first_name"
},
lastName: {
type: DataTypes.STRING,
field: "last_name"
},
username: {
type: DataTypes.STRING,
field: "username"
},
email: {
type: DataTypes.STRING,
field: "email"
},
password: {
type: DataTypes.STRING,
field: "password"
},
token: {
type: DataTypes.STRING,
field: "access_token"
},
isAdmin: {
type: DataTypes.BOOLEAN,
field: "isAdmin"
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods:{
usernameExists: function(username, _clb){

},
userExists: function(username, _clb){

},
signup: function(params, _clb){
var fullnm_splt = params.fullname.split(' ');
params.firstName = (fullnm_splt.length >2) ? fullnm_splt.slice(0, -1).join(" ") : fullnm_splt[0];
params.lastName = (fullnm_splt.length >2) ? fullnm_splt.slice(-1).join(" ") : fullnm_splt[1];

res.redirect("/users/" + params.username);


}
},
instanceMethods:{
isValidPassword: function(password, _clb){
var _self = this;
hash(password, function(err, password_encr){
if(err){
return _clb(err);
}
if(password_encr == _self.password){
return _clb(null, true);
}

return _clb(null, false);
});

}
}
});
};

models/Clients.js

var Sequelize = require("sequelize");

module.exports=function(sequelize, DataTypes){
return Clients = sequelize.define("Clients", {
id:{
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
client_name: {
type: DataTypes.STRING,
field: "client_name"
},
client_host_name: {
type: DataTypes.STRING,
field: "client_host_name"
},
ip: {
type: DataTypes.STRING,
field: "ip"
},
mac: {
type: DataTypes.STRING,
field: "mac"
},
previous_ip: {
type: DataTypes.STRING,
field: "previous_ip"
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods:{

},
instanceMethods:{

}
});
};

当我尝试使用下面的函数将某些内容保存到数据库时,它会在日志中显示 INSERT INTO 查询,但 db 文件中没有任何变化。

create_client:

Clients
.create({client_host_name: params.client_host_name, ip: params.ip, mac: params.mac})
.then(function(err){
res.send("OK");
});

我该如何解决这个问题?

最佳答案

我已经测试了您的应用程序,一切都插入正常,请查看https://github.com/finfort/SequelizeTestRepo

关于node.js - Sequelize 不同步 - 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973939/

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