NodeJS-Express4.0 error: Cannot read property & amp; #39; Store & amp; #39; of undefined

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.