Amazing node. js Reading Notes: mongodb database interaction, node. jsmongodb

Source: Internet
Author: User
Tags mongodb version

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.

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.