简体   繁体   English

使用Mongodb和nodejs设置登录

[英]Setting up Login with Mongodb and nodejs

I'm new to Mean stack and I've been following this Tutorial . 我是Mean stack的新手,我一直在关注这个教程 I also included the Facebook and Google api for the logins. 我还包括用于登录的Facebook和Google API。

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 在我的主文件中,我有:// 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: 在database.js中,我有这个:

     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. 当我注释掉(“var configDB = require('./ config / database.js');”)我的server.js中的require数据库时,我能够运行它并将它放在我的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. 我查了一下错误,我看到的是人们使用127.0.0.1并取得了成功。我尝试使用'mongodb':'// 127.0.0.1:27017/test',因为人们取得了最大的成功,但它仍然没有为我工作。 I changed it from mongodb: localhost:27017/test, localhost/test, and pretty much all the solutions I was able to find. 我从mongodb更改了它:localhost:27017 / test,localhost / test,以及我能找到的几乎所有解决方案。

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. 这一切都在localhost上完成。

And just to make sure about MongoDb. 而且只是为了确保MongoDb。 I have mongodb in my "app" file. 我的“app”文件中有mongodb。 In my C:, I made a file called Data/db for mongodb when I installed. 在我的C:中,我在安装时为mongodb创建了一个名为Data / db的文件。

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. 当我在我的应用程序文件中转到我的cmd - > mongodb(版本3.2)时,我可以使用mongod并让它等待连接。 When I use mongo, I'm able to connect to my "test" db. 当我使用mongo时,我能够连接到我的“测试”数据库。

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. 这应该可行,您需要URL上的'mongodb:'前缀,而您不需要指定端口。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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