An error occurred while using mongodb for Express.
// Settings. js
Module. exports = {
CookieSecret: "xxxx ",
Db: "dbname ",
Host: "localhost ",
}
// App. js
Var express = require ("express ");
Var settings = require ("./settings ");
Error code for loading connect-mongo:
Var MongoStore = require ("connect-mongo") (express );
App. use (express. session ({
Secret: Settings. cookieSecret,
Key: Settings. db,
Cookie: {maxAge: 1000*60*60*24*30}, // 30 days
Store: new External store ({
Db: Settings. db
})
}));
Error: TypeError: Cannot read property 'store' of undefined
The above is the expression of express <4
After express 4.0 (including 4.0) is written as follows:
Var session = require ("express-session ");
Var MongoStore = require ("connect-mongo") (session );
App. use (session ({
Secret: settings. cookieSecret,
Store: new External store ({
Db: settings. db
})
});
The package. json configuration is as follows:
{
"Name": "dbTest ",
"Version": "0.0.1 ",
"Private": true,
"Scripts ":{
"Start": "node./bin/www"
},
"Dependencies ":{
"Express ":"~ 4.2.0 ",
"Static-favicon ":"~ 1.0.0 ",
"Morgan ":"~ 1.0.0 ",
"Cookie-parser ":"~ 1.0.1 ",
"Body-parser ":"~ 1.0.0 ",
"Debug ":"~ 0.7.4 ",
"Ejs ":"~ 0.8.5 ",
"Mongodb ":"*",
"Connect-mongo ":"*",
"Express-session ":"*"
}
}
// If npm install is not executed in the directory, run npm install in the package. json directory after mongodb, connect-mongo, and express-session are added.
The above is the mongodb configuration in express. If you have not installed a mongodb database locally, please download and install it at http://www.mongodb.org/downloads.