Home  >  Article  >  Web Front-end  >  Amazing node.js reading notes: mongodb database interaction_node.js

Amazing node.js reading notes: mongodb database interaction_node.js

WBOY
WBOYOriginal
2016-05-16 16:25:051739browse

This week’s study is mainly about nodejs database interaction, and we used jade template to build a user verification website. Mainly because I 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 only supports linux, darwin, and freebsd systems, and the latest version supports wins.

2. After nodejs performs insert operation: Unable to read the result

Copy code The code is 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 appearance is that the redirection failed. The real situation is that the insertion into the database has been successful but the 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 function needs to be achieved by adding the third parameter {safe:ture}, that is, 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 code The code is as follows:

MongoStore = require('connect-mongo')

app.use(express.session({
Secret:settings.cookieSecret,
store:new MongoStore({
          db:settings.db
})
}));

The source code is as above. The reason is found to be based on different versions of Express, and the connect-mongo module is introduced in different ways. There is also a special reminder in its Readme.md.

Copy code 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 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 different versions, just modify it accordingly.

4. Summary

After studying this book, I know some features of nodejs and active foreign language websites. The update frequency of some popular sections in node also increases the difficulty of learning. This book can be considered an introduction. Next, I plan to learn the sails back-end framework through actual combat. Problems encountered during study are also recorded in notebooks.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn