在附件文件夹目录,server.js和api.js文件下面。程序没有运行,给我错误如:
ReferenceError: dbo is not defined
at C:\shivapp\mean_app\mandiapp\server\api.js:18:1
at Layer.handle [as handle_request] (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\layer.js:95:5)
at next (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\layer.js:95:5)
at C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\index.js:335:12)
at next (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\index.js:174:3)
at router (C:\shivapp\mean_app\mandiapp\node_modules\express\lib\router\index.js:47:12).
文件夹目录
文件> server.js
var express=require('express');
var bodyParser=require('body-parser');
var path=require('path');
var http = require('http');
var app=express();
var api= require('./server/api');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,'dist')));
app.use('/',api);
//8376884527
app.get('*'),(req,res) => {
res.sendFile(path.join(__dirname,'dist/index.html'));
}
var port= process.env.port || '3000';
app.set('port',port);
var server = http.createServer(app);
server.listen(port,()=>console.log('server running...'));
档案 - > api.js
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var dbo;
MongoClient.connect("mongodb://localhost:27017/mandiapp", function(err, db) {
if(err) throw err;
var dbo = db.db("mandiapp");
});
router.get('/users', function(req, res) {
dbo.collection("user").find({}).toArray(function(err, result) {
if (err) throw err;
res.json(result);
//dbo.close();
})
})
router.get('/recent', function(req, res) {
var collection = db.get().collection('comments')
collection.find().sort({'date': -1}).limit(100).toArray(function(err, docs) {
res.render('comments', {comments: docs})
})
})
module.exports = router
答案 0 :(得分:0)
您看到的错误是因为错误表明未定义变量dbo
。
您的问题是以下代码:
MongoClient.connect("mongodb://localhost:27017/mandiapp", function(err, db) {
if(err) throw err;
var dbo = db.db("mandiapp");
});
您已在回调函数中定义了变量dbo
,该函数在此范围之外不可用。你应该在其他地方定义它并在这里指定它。
如果不清楚,请参阅下面的修复。
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var dbo = undefined;
MongoClient.connect("mongodb://localhost:27017/mandiapp", function(err, db) {
if(err) throw err;
dbo = db.db("mandiapp");
});
router.get('/users', function(req, res) {
dbo.collection("user").find({}).toArray(function(err, result) {
if (err) throw err;
res.json(result);
//dbo.close();
})
})
router.get('/recent', function(req, res) {
var collection = db.get().collection('comments')
collection.find().sort({'date': -1}).limit(100).toArray(function(err, docs) {
res.render('comments', {comments: docs})
})
})
module.exports = router
您的代码中有更多的不受约束的变量,例如collection
获取处理程序中的db
和/recent
。
答案 1 :(得分:0)
请检查link
删除行: var dbo = db.db(" mandiapp");
var db = MongoClient.connect('mongodb://localhost:27017/mandiapp', function(err, db) {
if(err)
throw err;
console.log("connected to the mongoDB !");
});
db.collection("user").find({}).toArray(function(err, result) {
if (err) throw err;
res.json(result);
//dbo.close();
})
})