Great node.js Reading notes MongoDB database interaction _node.js

Source: Internet
Author: User
Tags mongodb mongodb version versions

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.