Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoDB连接到数据库_Node.js_Mongodb - Fatal编程技术网

Node.js MongoDB连接到数据库

Node.js MongoDB连接到数据库,node.js,mongodb,Node.js,Mongodb,我正在node.js中开发一个应用程序,我发现了连接mongodb的语法。我知道还有其他的方法,但是有人能给我解释一下这个方法吗。这里连接的是什么?函数中的回调参数是如何工作的?断言用于什么 var MongoClient = require('mongodb').MongoClient, assert = require('assert'); var url = 'mongodb://localhost:27017/myproject'; var mongoClient = new

我正在node.js中开发一个应用程序,我发现了连接mongodb的语法。我知道还有其他的方法,但是有人能给我解释一下这个方法吗。这里连接的是什么?函数中的回调参数是如何工作的?断言用于什么

var MongoClient = require('mongodb').MongoClient,
    assert = require('assert');

var url = 'mongodb://localhost:27017/myproject';

var mongoClient = new function () {

    this.connect= function(callback) {
        MongoClient.connect(url, function (err, db) {
            console.log("Connected successfully to server");
            callback(err, db);
        });
    }
}

module.exports = mongoClient;
这里连接的是什么

在运行此代码之前,先运行MongoDB服务器实例。服务器在一个端口(通常为端口27017)上运行。您试图连接的是通过node.js程序连接的服务器实例

函数中的回调参数是如何工作的

MongoClient.connect(url,函数(err,db){})

此函数尝试将端口27017处的MongoDB服务器连接到数据库myproject。它将连接结果存储在该函数的两个参数中。在错误参数中存储null如果连接成功,则存储错误的堆栈跟踪。在成功连接的情况下,its会在第二个参数中向您发送一个DB实例。 回调函数将相同的结果返回给调用函数

断言用于什么

var MongoClient = require('mongodb').MongoClient,
    assert = require('assert');

var url = 'mongodb://localhost:27017/myproject';

var mongoClient = new function () {

    this.connect= function(callback) {
        MongoClient.connect(url, function (err, db) {
            console.log("Connected successfully to server");
            callback(err, db);
        });
    }
}

module.exports = mongoClient;
它是节点中的一个模块。它的用途在这里以美丽的文字给出

在这一行中,您需要mongodb库

var url = 'mongodb://localhost:27017/myproject';
这是您连接的url。格式是mongodb://host_address:port/db_name

//Creating a function with the name mongoClient and exporting it.
var mongoClient = new function () {
  //Creating another called connect inside the mongoClient function. Which you will call from other place to connect to the db. 
  this.connect= function(callback) {
    //Here the mongo client library actually connecting to mongo server, and through the callback function it return err and db object. if the connection is successful only then you should return db.
    MongoClient.connect(url, function (err, db) {
        if(!err){
           console.log("Connected successfully to server");
           //Here the call back function of the function which call the connect function getting called.
           callback(err, db);
        }else{
          throw (err);
        }
    });
  }
}
//here you are exporting the mongoClient function.
module.exports = mongoClient;
从其他文件连接到mongo,如下所示

var mongoClient = require('relative path to this file');
mongoClient.connect(function(err, db){
  //now db is the connection object.
  console.log(db);
});

谢谢你,莎拉。我不明白最后一个回调(err,db)。因此,如果没有错误,该函数将从MongoClient获取db实例,并将结果返回给调用函数。那就是mongoClient。是吗?是的,没错。因此,
mongoclient
具有这两种结果。请注意这一点,以便清楚地了解回调是如何进行的