nodejs+express+ejs learning record

Record 1: Difference between mongodb v2.0.x version and v3.0.x version

//mongodb v2.0写法参考
var MongoClient = require('mongodb').MongoClient;
var url='mongodb://localhost:27017/blog';

MongoClient.connect(url,function(err,db){
  var collection=db.collection('user');
  collection.insert(user,function(err,result){
      //...
  });
  db.close();
});


//mongodb v3.0写法参考
var MongoClient = require('mongodb').MongoClient;
var url='mongodb://localhost:27017';

MongoClient.connect(url,function(err,client){
  var db=client.db('blog');
  var collection=db.collection('user');
  collection.insert(user,function(err,result){
      //...
  });
  client.close();
});

The comparison shows that the callback parameters in mongoClient.connect are different. v2.0 directly returns db, which can directly operate the database, while v3.0 returns client, which needs to locate the database client.db to be operated first ( 'blog') and then manipulate the document

Record 2: connect-flash (req.flash is not a function error)

Pay attention to the order in which app.use(flash()); loads the flash middleware, first require('connect-flash'), then app.use(session()); then app.use(flash()); and finally Configure the order of routes to

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326440474&siteId=291194637