Amazing node. js Reading Notes: mongodb database interaction, node. jsmongodb
This week, I learned how to interact with nodejs databases and used the jade template to build a user-verified website. I encountered several problems.
1. mongodb version is too low
Npm ERR! Not compatible with your operating system or architecture: mongodb@0.9.9
0.9.9 only supports linux, darwin, And freebsd systems. wins is supported in the latest version.
2. After nodejs performs the insert operation: The result cannot be read.
Copy codeThe Code is as follows:
App. post ('/signup', function (req, res, next ){
// Insert a document
App. users. insert (req. body. user, function (err, doc ){
If (err) return next (err );
Res. redirect ('/login/' + doc [0]. email );
});
});
The redirection fails. The actual situation is that the database has been inserted successfully, but the doc is empty. Let alone the doc [0]. email value. The reason is that insert operations are performed asynchronously. By default, asynchronous operations do not return their results to determine whether the operation is successful. You need to add the third parameter {safe: ture} to implement this function, that is, the app. users. insert (req. body. user, {safe: ture}, function (){......}). In this way, the result is read successfully.
3. undefined store appears in connect-connect
Copy codeThe Code is as follows:
External Store = require ('connect-mongo ')
App. use (express. session ({
Secret: settings. cookieSecret,
Store: new External store ({
Db: settings. db
})
}));
The source code is as above. Find out the cause for different Express-based versions. The connect-mongo module introduces different methods. In its Readme. md, a special prompt is also displayed.
Copy codeThe Code is as follows:
With express4:
Var session = require ('express-session ');
Var MongoStore = require ('connect-mongo ') (session );
App. use (session ({
Secret: settings. cookie_secret,
Store: new External store ({
Db: settings. db,
})
}));
With express <4:
Var express = require ('express ');
Var MongoStore = require ('connect-mongo ') (express );
App. use (express. session ({
Secret: settings. cookie_secret,
Store: new External store ({
Db: settings. db
})
}));
Modify the version accordingly.
4. Summary
After studying this book, I know some characteristics of nodejs and its active foreign languages. The update frequency of some popular sections in node also increases the difficulty of learning. This book is also an introduction. Next, we plan to learn the sails backend framework in practice. Problems encountered during learning are also recorded in the notebook.