This week's study is mainly Nodejs database interaction, and using the Jade template to do a user-validated site. Mainly encountered a few problems.
1.mongodb version is too low
NPM err! Not compatible with your operating system or architecture:mongodb@0.9.9
0.9.9 supports only LINUX,DARWIN,FREEBSD systems, and WINS is supported in the latest version.
2.nodejs after insert operation: unable to read results
Copy Code code as follows:
App.post ('/signup ', function (req, res, next) {
Insert Document
App.users.insert (Req.body.user, function (err, doc) {
if (ERR) return next (ERR);
Res.redirect ('/login/' + doc[0].email);
});
});
The image is redirect failure, the real situation is that the database has been inserted successfully but Doc is empty, not to mention the value of Doc[0].email. The reason is that operations such as insert are performed asynchronously, and asynchronous operations do not return their results by default to determine whether the operation is successful. This functionality needs to be implemented by adding the third parameter {safe:ture}, i.e. App.users.insert (Req.body.user, {safe:ture}, function () {...}). This will result in the successful reading of the results.
3.connect-connect Store Not defined
Copy Code code as follows:
Mongostore = require (' Connect-mongo ')
App.use (Express.session ({
Secret:settings.cookieSecret,
Store:new Mongostore ({
Db:settings.db
})
}));
Source code such as, to identify the reasons for the different versions based on Express, Connect-mongo modules introduced in different ways. In its readme.md also special hint.
Copy Code code as follows:
with express4:
var session = require (' express-session ');
var mongostore = require (' Connect-mongo ') (session);
App.use (session ({
secret: Settings.cookie_secret,
store:new mongostore ({
db:settings.db,
}) (
}));
with express<4:
var express = require (' Express ');
var mongostore = require (' Connect-mongo ') (express);
App.use (express.session ({
Secret:settings.cookie_secret,
&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; store:new mongostore ({
db: Settings.db
})
);
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. Node in some of the hot plate update frequency also increased the difficulty of learning, this book is also a primer. Next, you plan to learn sails back-end frameworks through combat. The problems encountered in the study are also recorded in the Notebook.