mongo-连接错误mongo-连接(会话)
mongo-connect error with mongo-connect(session)
四处搜索后,我在 code
中找不到关于此错误的任何解决方案或错误。我的节点 JS 应用程序中有我的 app.js
文件,其中包含 mongo-connect
声明:
const MongoStore = require('connect-mongo')(session)
我遇到了这个错误:
TypeError: Class constructor MongoStore cannot be invoked without
'new'
at Object. (/Users/souhailmohamed/project/devops/story website/app.js:11:20)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
下面是我的 app.use
代码:
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
我很了解
const MongoStore = require('connect-mongo')(session)
但是我没有理解错误。但它来自 traversy media LinK
的 youtube 教程
好的,所以我找到了解决它的方法,我不知道原因,在哪个版本的 express 上使用,但我在 mongo-connect 之后删除了(会话)并更改了app.use 到此 :
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: 'mongodb+srv://<id+ password>@cluster0.cq7f2.mongodb.net/DBname?retryWrites=true&w=majority' })
}))
我也是这样,所以我发现了一个 npm pakage connect-mongodb-session。
安装它并使用与 Brad 在他的视频中使用的相同的会话代码
const MongoDBStore = require('connect-mongodb-session')(session);
//sessions
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUnitialized: false,
store: new MongoDBStore({
mongooseConnection: mongoose.connection
})
//cookie: { secure: true }
}))
const store = new MongoStore({
uri: 'your mongo uri' ,
collection:'mySessions'
})
//express middleware
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: store
// Don't create a session until something is stored
// cookie: { secure: true } - this wont work without https
}))
它也解决了我的重新登录问题。如果您关闭选项卡并租用本地主机,您已经登录
我在我的代码中遇到了同样的问题并使用这个解决了它:
const MongoDbStore = require('connect-mongo');
从 require 语句中删除 (session) 并像这样更新 app.use() :
// Session Config
app.use(
session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoDbStore.create({
mongoUrl: YourDatabaseURL
})
})
);
解决方法如下:
const session = require('express-session');
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongoUrl: mongoose.connection._connectionString,
mongoOptions: {}
})
}))
我有同样的问题。这是解决方案:
var session = require('express-session');
var MongoStore = require('connect-mongo');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
app.use(session({
secret : 'mysecretkey',
resave : true,
saveUninitialized : true,
store :MongoStore.create({ mongoUrl: DBUrl })
}));
connect-mongo
v4 引入了新签名(与 v3 相比)。
这里是官方migration guide.
此外,这里是我在特定项目(使用 mongoose
and dotenv
)中从 connect-mongo
v3 升级到 v4 所做更改的快速差异:
“connect-mongo”文档here
中语法的某些规则已更改
const MongoStore = require("connect-mongo");
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI //(URI FROM.env file)
})
//stroed it in database
}));
app.use(session({
store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' })
}));
// Advanced usage
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://user12345:foobar@localhost/test-app?authSource=admin&w=1',
mongoOptions: advancedOptions // See below for details
})
}));
在您的 app.js
中进行以下更改:
let MongoStore = require('connect-mongo');
app.use(
session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: 'your mongo url'
})
});
);
参考文献:
只需使用另一个 npm 包 connect-mongodb-session 就可以了
https://www.npmjs.com/package/connect-mongodb-session
const MongoDBStore = require('connect-mongodb-session')(session);
四处搜索后,我在 code
中找不到关于此错误的任何解决方案或错误。我的节点 JS 应用程序中有我的 app.js
文件,其中包含 mongo-connect
声明:
const MongoStore = require('connect-mongo')(session)
我遇到了这个错误:
TypeError: Class constructor MongoStore cannot be invoked without 'new' at Object. (/Users/souhailmohamed/project/devops/story website/app.js:11:20) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47
下面是我的 app.use
代码:
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
我很了解
const MongoStore = require('connect-mongo')(session)
但是我没有理解错误。但它来自 traversy media LinK
的 youtube 教程好的,所以我找到了解决它的方法,我不知道原因,在哪个版本的 express 上使用,但我在 mongo-connect 之后删除了(会话)并更改了app.use 到此 :
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: 'mongodb+srv://<id+ password>@cluster0.cq7f2.mongodb.net/DBname?retryWrites=true&w=majority' })
}))
我也是这样,所以我发现了一个 npm pakage connect-mongodb-session。 安装它并使用与 Brad 在他的视频中使用的相同的会话代码
const MongoDBStore = require('connect-mongodb-session')(session);
//sessions
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUnitialized: false,
store: new MongoDBStore({
mongooseConnection: mongoose.connection
})
//cookie: { secure: true }
}))
const store = new MongoStore({
uri: 'your mongo uri' ,
collection:'mySessions'
})
//express middleware
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: store
// Don't create a session until something is stored
// cookie: { secure: true } - this wont work without https
}))
它也解决了我的重新登录问题。如果您关闭选项卡并租用本地主机,您已经登录
我在我的代码中遇到了同样的问题并使用这个解决了它:
const MongoDbStore = require('connect-mongo');
从 require 语句中删除 (session) 并像这样更新 app.use() :
// Session Config
app.use(
session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoDbStore.create({
mongoUrl: YourDatabaseURL
})
})
);
解决方法如下:
const session = require('express-session');
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongoUrl: mongoose.connection._connectionString,
mongoOptions: {}
})
}))
我有同样的问题。这是解决方案:
var session = require('express-session');
var MongoStore = require('connect-mongo');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
app.use(session({
secret : 'mysecretkey',
resave : true,
saveUninitialized : true,
store :MongoStore.create({ mongoUrl: DBUrl })
}));
connect-mongo
v4 引入了新签名(与 v3 相比)。
这里是官方migration guide.
此外,这里是我在特定项目(使用 mongoose
and dotenv
)中从 connect-mongo
v3 升级到 v4 所做更改的快速差异:
“connect-mongo”文档here
中语法的某些规则已更改const MongoStore = require("connect-mongo");
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI //(URI FROM.env file)
})
//stroed it in database
}));
app.use(session({
store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' })
}));
// Advanced usage
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://user12345:foobar@localhost/test-app?authSource=admin&w=1',
mongoOptions: advancedOptions // See below for details
})
}));
在您的 app.js
中进行以下更改:
let MongoStore = require('connect-mongo');
app.use(
session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: 'your mongo url'
})
});
);
参考文献:
只需使用另一个 npm 包 connect-mongodb-session 就可以了 https://www.npmjs.com/package/connect-mongodb-session
const MongoDBStore = require('connect-mongodb-session')(session);