简体   繁体   中英

How do I install, and connect to mongodb in node.js on my remote server (ubuntu)?

I have spent at least 3 hours failing to connect to mongodb onto my server. I have managed to install it, create my database, and i have created a new user in the console:

use admin

then

db.createUser(
  {
    user: "admin",
    pwd: "xxx",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

And all seems good. Then, to connect to the user in app.js, i do:

var databaseUrl = "mongodb://admin:xxx@108.61.221.63:27018/mydatabasename";
var db = require("mongodb").connect(databaseUrl);

At this point, when i run the server (npm start) this is what i get:

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
/home/adminftp/public_html/globular/node_modules/mongodb/lib/mongo_client.js:92
    throw new Error("no callback function provided");
          ^
Error: no callback function provided
    at Function.MongoClient.connect (/home/adminftp/public_html/globular/node_modules/mongodb/lib/mongo_client.js:92:11)
    at Object.<anonymous> (/home/adminftp/public_html/globular/app.js:31:29)
    at Module._compile (module.js:462:26)
    at Object.Module._extensions..js (module.js:480:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:503:10)
    at startup (node.js:132:16)
    at node.js:817:3

...i have no idea what to make of it.

If anyone has any ideas how i can resolve it i would appreciate it. Thank you!

Looking at the documentation ( 1.x docs or v2.x docs ), the connect function doesn't return a db object, it takes a callback function as an argument. This function will be called with the db as its second parameter.

This matches with the 'no callback function provided' error you are getting.

So, based on the examples in the documentation, you might try:

var MongoClient = require('mongodb').MongoClient;
var databaseUrl = "mongodb://admin:xxx@108.61.221.63:27018/mydatabasename";

MongoClient.connect(databaseUrl, function(err, db) {
    if (err) {
        // do something with the error
    }

    // do something with the db 
});

That said, looking at the source code for the driver, I think that the connect method is also directly aliased from what is returned from the require, so in your example you may be able to use:

require('mongodb').connect(databaseUrl, function(err, db) { 
   ...
});

But I would recommend following the examples in the docs.

As other posts have indicated, there are other wrappers around mongodb. mongoose is very nice and well supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM