This article mainly introduces the amazing node. js reading notes: mongodb database interaction. if you need it, you can refer to the next week's study. it is mainly about nodejs database interaction, and use 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.
The 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
The 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.
The 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.