繁体   English   中英

mongodh + hapi.js:集合未定义?

[英]mongodh + hapi.js: Collection is not defined?

我是 mongodb 和 Hapi.js 的新手。 我正在尝试为读取请求创建 API,但不确定如何在 server.route 中编写处理程序方法。

这是我使用 hapi 配置我的 mongoclient 的方法:

 '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 })

转到主页路径工作正常,但是当我转到 ./api/tours 路径时,在终端中出现以下错误:

 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)

如何正确定义集合? 谢谢你。

您的错误消息意味着collection超出了处理程序的范围。 您在tours function声明它。

但是您在如何使用 Mongoclient 处理数据库和集合时也遇到了一个小错误。

让我向您展示在保持一般设置的同时它是如何工作的。 在那里你可以看到db现在可以被处理程序访问。

 '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 })

我了解这只是您这边的一个学习示例。 但也许您想考虑从最新的 hapijs 版本开始: 17. 涉及一些更大的变化,现在从该版本开始让您的生活更轻松。 你的短代码已经有很多嵌套的回调。 版本 17 将支持使用 await/async。

暂无
暂无

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

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