2016-06-25 10 views
0

enter image description here私はnodeを学び、mongoDBをデータベースとして使用していくつかのチュートリアルを使用してアプリケーションを構築しようとしています。私はこれらの2つのjavascriptファイル、mongo dbに接続するためのdatabase.js、およびindex.jsのseedDatabase()関数を使用して、私が持っているデータをシードします。mongodbへの接続中にノードアプリケーションのエラーが発生しました

database.js:

(function (database) { 

var mongodb = require("mongodb"); 
var mongoUrl = "mongodb://localhost:27017/theBoard"; 
var theDb = null; 

database.getDb = function (next) { 
    if (!theDb) { 
     // connect to the database 
     mongodb.MongoClient.connect(mongoUrl, function (err, db) { 
      if (err) { 
       next(err, null); 
      } else { 
       theDb = { 
        db: db, 
        notes: db.collection("notes") 
       }; 
      } 
     }); 
    } else { 
     next(null, theDb); 
    } 
} 

}); 

index.js:

(function (data) 
{ 
    var seedData = require("./seedData"); 
    var database = require("./database.js"); 

    data.getNoteCaregories = function (next) 
    { 
     next(null, seedData.initialNotes); 
    }; 

    function seedDatabase() { 
     database.getDb(function (err, db) { 
      if (err) { 
       console.log("Failed to seed database: " + err); 
      } else { 
       // test to see if data exists yet 
       db.notes.count(function (err, db) { 
        if (err) { 
         console.log("Failed to retrieve database count."); 
        } else { 
         if (count == 0) { 
          console.log("Seeding the Database..."); 
          seedData.initialNotes.forEach(function (item) { 
           db.notes.insert(item, function (err) { 
            if (err) 
             console.log("Failed to insert note into database"); 
           }); 
          }); 
         } else { 
          console.log("Database already seeded"); 
         } 
        } 
       }); 
      } 
     }); 
    } 

    seedDatabase(); 

})(module.exports); 

答えて

0

あなたdatabase.jsファイル内の関数getDbまたはオブジェクトdatabaseをエクスポートしないためにです。

exports.getDb = function (next) { 
    if (!theDb) { 
     // connect to the database 
     mongodb.MongoClient.connect(mongoUrl, function (err, db) { 
      if (err) { 
       next(err, null); 
      } else { 
       theDb = { 
        db: db, 
        notes: db.collection("notes") 
       }; 
      } 
     }); 
    } else { 
     next(null, theDb); 
    } 
} 
:そのような何かのために

更新しますdatabase.js

関連する問題