简体   繁体   中英

Setting up Login with Mongodb and nodejs

I'm new to Mean stack and I've been following this Tutorial . I also included the Facebook and Google api for the logins.

The problem that I'm running into right now is when I use the database, it gives me this error.

 events.js:141
 throw er; // Unhandled 'error' event
 ^

Error: failed to connect to [undefined:27017]
at null.<anonymous> (C:\Users\App\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:556:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (C:\Users\App\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:156:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\App\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:534:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:1012:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)

In my main file I have: // server.js

    //Set up
    var express  = require('express');
    var app      = express();
    var port     = process.env.PORT || 8080;
    var mongoose = require('mongoose');
    var passport = require('passport');
    var flash    = require('connect-flash');

    var morgan       = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser   = require('body-parser');
    var session      = require('express-session');

    var configDB = require('./config/database.js');

     // configuration  ==============================================================
    //mongoose.connect('mongodb://127.0.0.1:27017/test');
      mongoose.connect(configDB.url); // connect to our database
    //mongoose.connect(configDB);

    require('./config/passport')(passport); // pass passport for configuration

    (rest of the code from the tutorial) 

In the database.js, I have this:

     module.exports = {
     'mongodb':'//127.0.0.1:27017/test'
     };

When I comment out( " var configDB = require('./config/database.js');") the require database in my server.js, I'm able to run it and have it on my localhost. However, when I try to sign up, the page just will just keep loading then crash. So I'm guessing it has to be something with the database.

I looked up the error and all I see is that people using 127.0.0.1 and having success.I tried using 'mongodb':'//127.0.0.1:27017/test' since people had the best success but it still doesn't work for me. I changed it from mongodb: localhost:27017/test, localhost/test, and pretty much all the solutions I was able to find.

So now I'm here stuck and wondering how to fix this. Is there some error in how I'm routing it to my database that I'm getting this error? This is all done on localhost.

And just to make sure about MongoDb. I have mongodb in my "app" file. In my C:, I made a file called Data/db for mongodb when I installed.

When I go to my cmd -> mongodb(ver 3.2) in my app file, I'm able to use mongod and have it wait for a connection. When I use mongo, I'm able to connect to my "test" db.

Any advice/help would be much appreciated. Thank you.

This should work, you need the 'mongodb:' prefix on the URL, and you don't need to specify the port.

module.exports = {
    'mongodb':'mongodb://localhost/test'
};

https://docs.mongodb.com/manual/reference/connection-string/

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