我使用护照尝试了nodejs登录教程。
deserializeUser在由c9.io
托管时运行良好但是由openshift托管它会产生错误
TypeError: hex is not a function
at Function.from (native)
at Function.from (native)
at new ObjectID (/opt/app-root/src/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid.js:52:32)
at ObjectID (/opt/app-root/src/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid.js:31:42)
at /opt/app-root/src/app.js:120:17
at pass (/opt/app-root/src/node_modules/passport/lib/authenticator.js:347:9)
at Authenticator.deserializeUser (/opt/app-root/src/node_modules/passport/lib/authenticator.js:352:5)
at SessionStrategy.authenticate (/opt/app-root/src/node_modules/passport/lib/strategies/session.js:53:28)
at attempt (/opt/app-root/src/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/opt/app-root/src/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/opt/app-root/src/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/app-root/src/node_modules/express/lib/router/index.js:312:13)
at /opt/app-root/src/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/opt/app-root/src/node_modules/express/lib/router/index.js:330:12)
at next (/opt/app-root/src/node_modules/express/lib/router/index.js:271:10)
at initialize (/opt/app-root/src/node_modules/passport/lib/middleware/initialize.js:53:5)
我的deserializeUser代码就像这样
var express = require("express"),
app = express(),
cookieParser = require("cookie-parser"),
bodyParser = require('body-parser'),
assert = require('assert'),
fs = require("fs"),
XLSX = require("xlsx"),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
multer = require("multer"),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
mongodb = require('mongodb'),
ObjectId = require('mongodb').ObjectId;
passport.serializeUser(function(user, done) {
console.log("serialize:%s",user._id);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
if (!db) {
initDb(function(err){});
}
if (db){
var col = db.collection("users");
col.findOne(ObjectId(id), function(err, user) {
console.log("deserialize:%s",user.username);
return done(err, user);
});
}
});
该行(/opt/app-root/src/app.js:120:17)是
col.findOne(ObjectId(id), function(err, user) {
我认为函数ObjectId不适用于openshift环境
这只是一个错误吗?为什么在c9.io上工作而不是在openshift上工作?
答案 0 :(得分:0)
我今天得到了与某些mongodb-core更新相关的类似错误。
我通过将mongodb-core强制转换为我以前的版本来修复它:
npm install --save mongodb-core@1.3.18