gpt4 book ai didi

node.js - 用户被覆盖,但仅有时在 Node.js/Mongoose/Passport/Connect-Mongo 应用程序中

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

我正在使用 Heroku/Mongolab (并且我已经使用 MongoHQ 尝试过),标题显示了我的设置。这是我的 package.json:

"name": "metatf-web",
"version" : "0.0.3",
"dependencies": {
"express": "*",
"ejs": ">=0.4.2",
"passport": "*",
"passport-steam": "*",
"validator": "*",
"mongoose" : "*",
"connect-mongo" : "latest",
"mongodb" : "latest"
}

基本上,当我创建新用户时,我的数据库会覆盖数据库中的用户,但只是有时。这是我的用户架构:

/**
* Module dependencies
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

/**
* User Schema
*/
module.exports = function() {
var Users = new Schema({
steamid : { type: String }
, regdate : { type: Date, default: Date.now }
, avatar : { type: String, default: "none" }
, isadmin : { type: String, default: "no" }
});
mongoose.model("Users", Users);
};

这是我用来确定用户是否在数据库中的函数,如果不在数据库中则确定:

var checkIfUserAddToDbIfNot = function(req, res, next) {
var steamID = req.user;
require('./controllers/user_controller').get(steamID, function(err, doc) {
console.log(doc);
if (!doc) { // User not found
require('./controllers/user_controller').create(steamID);
console.log('User added');
}
});
return next();
};

还有我的 user_controller (CRUD):

/**
* Module dependencies.
*/
var mongoose = require('mongoose')
, Users = mongoose.model("Users")
, user = new Users();

// Create
exports.create = function (steamID) {
if (steamID == 76561197991291041) { // Add me as an admin for when database resets.
user.isadmin = 'yes';
} else {
user.isadmin = 'no';
}
user.steamid = steamID;
user.save(function (err, callback) {
if (err) throw err;
console.log('User ' + user.steamid + ' added');
console.log('Admin: ' + user.isadmin);
});
};

// Read
exports.get = function (steamID, fn) {
Users.findOne({ steamid: steamID }, function (err, doc) {
if (err) return err;
if (doc) {
fn(null, doc);
} else {
fn(null, null); // User not found
}
});
};

这里是调用检查用户函数的地方:

app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }), checkIfUserAddToDbIfNot, function (req, res) {
res.redirect('/');
});

最后这是我的 connect-mongo 东西:

app.use(express.session({
secret: 'dont be walmarting',
store: new MongoStore(sess_conf.db),
cookie: {
maxAge: new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
},
maxAge : new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
}));

这里的任何帮助将不胜感激,我一生都无法弄清楚为什么用户被覆盖。我尝试在网站中添加新用户之前清除 cookie,因为我认为这是 Connect-Mongo session ,但这不起作用。感谢任何可以帮助我的人。

编辑:我正在下面初始化 Passport session :

// Initialize Passport!  Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
// END APP CONFIG (annotated)

最佳答案

我认为这是你的问题:

var mongoose = require('mongoose')
, Users = mongoose.model("Users")
, user = new Users(); // <-- this creates a module variable `user`, but only once
// (the first time the file is require()'d)

// Create
exports.create = function (steamID) {

// ...which is being re-used every time this function is called.
// Instead, you need to create the new user here:
var user = new Users;

if (steamID == 76561197991291041) { // Add me as an admin for when database resets.
user.isadmin = 'yes';
} else {
user.isadmin = 'no';
}
user.steamid = steamID;
user.save(function (err, callback) {
if (err) throw err;
console.log('User ' + user.steamid + ' added');
console.log('Admin: ' + user.isadmin);
});
};

关于node.js - 用户被覆盖,但仅有时在 Node.js/Mongoose/Passport/Connect-Mongo 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15579233/

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