This week's learning is mainly on nodejs' database interaction, and we use the Jade template to make a user-verified website together. Mainly, I encountered several problems.
1. Mongodb version is too low
npm ERR! Not compatible with your operating system or architecture: [email protected]
0.9.9 only supports linux, darwin, and freebsd systems, and the latest version supports wins.
2. After nodejs performs insert operation: the result cannot be read
The code copy is as follows:
app.post('/signup', function(req, res, next){
//Insert the document
app.users.insert(req.body.user, function(err, doc){
if(err) return next(err);
res.redirect('/login/' + doc[0].email);
});
});
The appearance is that the redirection failed, and the real situation is that the insertion of the database has been successful but the doc is empty, let alone the value of doc[0].email. The reason is that operations like insert are performed asynchronously. Asynchronous operations do not return their results by default to determine whether they are running successfully. This function needs to be implemented by adding the third parameter {safe:ture}, namely app.users.insert(req.body.user, {safe:ture}, function(){...}). This way, the result will be read successfully.
3. Connect-connect undefined store appears
The code copy is as follows:
MongoStore = require('connect-mongo')
app.use(express.session({
secret:settings.cookieSecret,
store:new MongoStore({
db:settings.db
})
}));
The source code is as above. The reason is found that the connect-mongo module is introduced differently based on different versions of Express. There is also a special reminder in its Readme.md.
The code copy is as follows:
With expression4:
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 expression<4:
var express = require('express');
var MongoStore = require('connect-mongo')(express);
app.use(express.session({
secret: settings.cookie_secret,
store: new MongoStore({
db: settings.db
})
}));
For different versions, please modify them accordingly.
4. Summary
After studying this book, I know some of the characteristics of nodejs and the active foreign language website. The frequency of updates of some popular sections in node has also increased the difficulty of learning, so this book can be considered an introductory one. Next, we plan to learn sails backend framework through practical combat. The problems encountered during study are also recorded in the notebook.