2016-09-01 9 views
0
var express = require('express'); 
var GoogleUrl = require('google-url'); 
var favicon = require('serve-favicon'); 
var mongo = require('mongodb').MongoClient; 
var app = express(); 
var db; 
var googleUrl = new GoogleUrl({key: '********'}); 
var PORT = 8080; 


mongo.connect('mongodb://localhost:27017/url-shortener', function(err, db){ 
    if(err){ 
     throw new Error('Database failed to connect'); 
    } else{ 
     console.log('Successfully connected to MongoDB on port 27017'); 
    } 
    db.createCollection('sites', { 
     autoIndexID: true 
    }); 
    db.close(); 
}); 

app.use(favicon(__dirname+'/public/favicon.ico')); 


app.get('/new/*', function(req, res){ 
    console.log('This is the url: '+req.params[0]); 
    googleUrl.shorten(req.params[0], function(err, shortUrl){ 
     if(err){ 
      console.log(err); 
     }else{ 
      console.log(shortUrl); 
     } 
     check_db(req.params[0], shortUrl, db); 
    }); 
}); 


app.listen(PORT, function(){ 
    console.log('Express listening on: '+PORT); 
}); 

//When I try to call my db.collection method here, I get an error statement saying that TypeError: Cannot read property 'collection' of undefined.以下のコードでdb.collectionにアクセスしようとすると、定義されないのはなぜですか?

function check_db(longUrl, shortUrl, db){ 
    db.collection('sites').findOne({ 
     'longUrl': longUrl, 
     'shortUrl': shortUrl 
    }, function(err, result){ 
     if(err){ 
      throw new Error(err); 
     }if(result){ 
      console.log('This site already exists on the database'); 
     }else{ 
      console.log('This site does not exist on the database'); 
     } 
    }); 
} 

I have made sure to declare my db variable globally and I even passed it to the check_db function to make sure.

Following is the error statement in full

TypeError: Cannot read property 'collection' of undefined at check_db (/home/ubuntu/workspace/urlshortener/server.js:45:7) at /home/ubuntu/workspace/urlshortener/server.js:35:8 at /home/ubuntu/workspace/urlshortener/node_modules/google-url/lib/google.js:18:7 at Immediate._onImmediate (/home/ubuntu/workspace/urlshortener/node_modules/google-url/lib/google.js:163:11) at processImmediate [as _immediateCallback] (timers.js:383:17)

+0

変数dbを初期化する場所がわかりません –

+0

Dbはmongoシェルのグローバル変数ですが、ノードjsではありません。 – notionquest

+0

@ Mr.7 - 私はプログラムの6行目で初期化しています。 –

答えて

1

あなたのDBは未定義です。接続コールバックの作成されたdbに割り当てる必要があります。

mongo.connect('mongodb://localhost:27017/url-shortener', function(err, newDb){ 
    if(err){ 
     throw new Error('Database failed to connect'); 
    } else{ 
     console.log('Successfully connected to MongoDB on port 27017'); 
    } 
    db = newDb; // ADD THIS 
    db.createCollection('sites', { 
     autoIndexID: true 
    }); 
    // db.close(); 
}); 

EDIT:

@notionquestの答えで指摘したように、あなたもあなたの要求が行われるまで、オープン接続を維持する必要があります。

+0

あなたのソリューションはうまくいきました!ありがとう!しかし、私は別の問題を今私のcheck_db関数には、コンソールのステートメントは、アプリケーションによって記録されていないです。 findOneメソッドに入りますが、コールバックには入りません。私はデバッガで実行しようとしましたが、運はありません! –

1

あなたは次の行にMongoの接続を取得: - その後

mongo.connect('mongodb://localhost:27017/url-shortener', function(err, db){ 

は、その内部の接続を閉じて: -

db.close(); 

このCLOSE文をコメントしてみてください。私はそれがうまくいくと思う。

同じ接続を使用したい場合(これはよい方法です)、接続を閉じないでください。最後に接続を閉じることができます。

関連する問題