The great node.js book notes of the mongodb database interaction

  • 2020-05-09 18:05:33
  • OfStack

This week's study of   mainly focuses on the database interaction of nodejs, and USES jade template 1 to make a user verified website. I have encountered several problems.

1. The 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. The latest version already supports wins.

2.nodejs: unable to read the result after insert operation


 app.post('/signup', function(req, res, next){
      // Inserted into the document
      app.users.insert(req.body.user, function(err, doc){
           if(err) return next(err);
           res.redirect('/login/' + doc[0].email);
      });
 });

  looks like a redirection failure, but the reality is that the insert into the database has been successful and doc is empty, not to mention the value of doc[0].email. The reason is that operations like insert are done asynchronously, and asynchronous operations by default do not return their results to determine if they were run successfully. This is done by adding a third parameter {safe:ture}, app.users.insert (req.body.user, {safe:ture}, function(){... }). The result is read successfully.

3. connect-connect has not defined store


 MongoStore = require('connect-mongo')
 
 app.use(express.session({
     secret:settings.cookieSecret,
     store:new MongoStore({
         db:settings.db
     })
 }));

Source code as above, find out the reason is that the connect-mongo module is introduced in different ways based on different versions of Express. Readme.md is also specially noted in its Readme.md.


 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,
         store: new MongoStore({
           db: settings.db
         })
       }));

For the different version, the corresponding modification can be.

4. To summarize

After the study of this book,   knows some features of nodejs and active foreign language station. The frequency with which some of the popular sections in node are updated also makes learning more difficult. This book is also an introduction. The next step is to learn the sails back-end framework in the real world. The problems encountered in the study are also recorded in the notebook.


Related articles: