简体   繁体   中英

mongodh + hapi.js: Collection is not defined?

I am new to mongodb and Hapi.js. I am trying to create an API for read requests, but am not sure how to write the handler method in server.route.

Here's how I have my mongoclient configured with hapi:

 'use strict'; var MongoClient = require('mongodb').MongoClient; //using version 3.x var Hapi = require('hapi');//using v16 var url = 'mongodb://****:****@ds131687.mlab.com:31687/learning_mongo'; var db; var server = new Hapi.Server(); server.connection({ port:8080 }); server.route( [ // Get tour list { method: 'GET', path: '/api/tours', handler: function(request, reply){ collection.find().toArray(function(err,tours){ reply(tours); }); } }, // Home page { method: 'GET', path: '/', handler: function(request, reply) { reply( "Hello world from Hapi/Mongo example."); } } ]); var tours = function(db, callback) { var collection = db.collection('tours'); collection.find().toArray(function(err, docs){ console.log(docs); callback; }); }; MongoClient.connect(url, function(err,client) { server.start(function(err) { tours(client.db('learning_mongo'), function(){ console.log('Hapi is listening to http://localhost:8080'); client.close(); }); });//end server })

Going to the homepage path works fine, but when I go to ./api/tours path, I get the following error in terminal:

 Debug: internal, implementation, error ReferenceError: Uncaught error: collection is not defined at handler (/home/ubuntu/workspace/index.js:22:13) at Object.internals.handler (/home/ubuntu/workspace/node_modules/hapi/lib/handler.js:101:51) at request._protect.run (/home/ubuntu/workspace/node_modules/hapi/lib/handler.js:32:23) at module.exports.internals.Protect.internals.Protect.run (/home/ubuntu/workspace/node_modules/hapi/lib/protect.js:60:12) at exports.execute (/home/ubuntu/workspace/node_modules/hapi/lib/handler.js:26:22) at each (/home/ubuntu/workspace/node_modules/hapi/lib/request.js:401:16) at iterate (/home/ubuntu/workspace/node_modules/items/lib/index.js:36:13) at done (/home/ubuntu/workspace/node_modules/items/lib/index.js:28:25) at module.exports.internals.Auth.internals.Auth._authenticate (/home/ubuntu/workspace/node_modules/hapi/lib/auth.js:222:16) at internals.Auth.authenticate (/home/ubuntu/workspace/node_modules/hapi/lib/auth.js:197:17)

How do I correctly define the collection ? Thank you.

Your error message means that collection is out of scope inside the handler. You declare it inside the tours function .

But you also got have a minor error how you approach the database and the collection with the Mongoclient.

Let me show you how it would work while keeping your general set-up. There you can see that db can be accessed by the handler now.

 'use strict'; var MongoClient = require('mongodb').MongoClient; //using version 3.x var Hapi = require('hapi'); //using v16 var url = 'mongodb://****:****@ds131687.mlab.com:31687/'; var db; var server = new Hapi.Server(); server.connection({ port: 8080 }); server.route([ // Get tour list { method: 'GET', path: '/api/tours', handler: function(request, reply) { db.collection('tours').find().toArray(function(err, tours) { reply(tours); }); } }, // Home page { method: 'GET', path: '/', handler: function(request, reply) { reply("Hello world from Hapi/Mongo example."); } } ]); var tours = function(db, callback) { db.collection('tours').find().toArray(function(err, docs) { console.log(docs); callback; }); }; new MongoClient.connect(url, function(err, client) { db = client.db('learning_mongo') server.start(function(err) { tours(db, function() { console.log('Hapi is listening to http://localhost:8080'); client.close(); }); }); //end server })

I understand that this is only a learning example from your side. But maybe you want to consider starting with the latest hapijs version: 17. There are some bigger changes involved and it makes your life easier starting with that version now. Your short code has already lot of nested callbacks. Version 17 will support using await/async.

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