This week's study is mainly Nodejs database interaction, and uses the jade template together to do a user-authenticated site. Mainly encountered a few problems.
1.mongodb version too low
NPM err! Not compatible with your operating system or architecture: [email protected]
0.9.9 only support LINUX,DARWIN,FREEBSD these systems, the latest version has supported wins.
2.nodejs after insert operation: unable to read results
1 app.post ('/signup ', function Span style= "color: #000000;" > (req, res, next) { 2 // insert document 3 App.users.insert (Req.body.user, function (err, doc) { 4 if (Err) return next (err); 5 res.redirect ('/login/' + doc[06 }); 7 });
The presentation is a redirect failure, the real situation is that inserting the database has been successful but Doc is empty, not to mention the value of Doc[0].email. The reason is that the insert operation is asynchronous, and the asynchronous operation defaults to not returning its results to determine if the operation was successful. This function needs to be implemented by adding the third parameter {safe:ture}, which is App.users.insert (Req.body.user, {safe:ture}, function () {...}). This will result in a successful read.
3.connect-connect an undefined store appears
1 mongostore = require (' Connect-mongo ')23app.use (express.session ({ 4 Secret:settings.cookieSecret,5 Store:new mongostore ({6 db:settings.db7 }) (8 }));
Source code as above, to find out the reason for different versions based on Express, Connect-mongo module introduced in different ways. Special hints are also made in their readme.md.
1 With EXPRESS4:2 3 varSession = require (' express-session ');4 varMongostore = require (' Connect-mongo ')) (session);5 6 App.use (Session ({7 Secret:settings.cookie_secret,8StoreNewMongostore ({9 db:settings.db,Ten }) One })); A -With Express<4: - the varExpress = require (' Express '); - varMongostore = require (' Connect-mongo ')) (express); - - App.use (express.session ({ + Secret:settings.cookie_secret, -StoreNewMongostore ({ + db:settings.db A }) at}));
For different versions, the corresponding changes can be.
4. Summary
After studying this book, I know some features of Nodejs and the active foreign language station. The update frequency of some of the hot plates in node also increases the difficulty of learning, and this book is also a primer. Next, we plan to learn sails back-end framework through combat. The problems encountered in learning are also recorded in notebooks.