Nodejs变量声明

时间:2015-01-06 17:55:55

标签: javascript node.js mongodb mongoose

我是Nodejs和javascript的新手。我已经开始使用快速发生器的项目,并安装了mongoose pacakge。

在routes / index.js文件上的

我有这个代码

 var express = require('express');
 var router = express.Router();
 var mongoose = require('mongoose');
 mongoose.connect('mongodb://localhost/test');
 var db = mongoose.connection;
 db.on('error', console.error.bind(console, 'connection error:'));
 db.once('open', function (callback) {
    var toSchema = mongoose.Schema({
       name: String,
       state : Boolean
    });
    var listing = mongoose.model('todoSchema', toSchema)
 });

 /* GET home page. */
 router.get('/', function(req, res) {
   var silence = new listing({ name: 'Silence', state:true })
   console.log(silence.name) // 'Silence'
   res.render('index', { title: silence.name });
 });
module.exports = router;

但是当我启动服务器时,我收到错误

listing is not defined

 ReferenceError: listing is not defined
at module.exports (D:\xampp\htdocs\nodeproject\myapp\routes\index.js:18:20)
at Layer.handle [as handle_request]
  D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\layer.js:82:5)
   at next (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\route.js:100:13)
   at Route.dispatch (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\route.js:81:3)
at Layer.handle [as handle_request] (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\layer.js:82:5)
at D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\index.js:235:24
at Function.proto.process_params (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\index.js:313:12)
at D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\index.js:229:12
at Function.match_layer (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\index.js:296:3)
at next (D:\xampp\htdocs\nodeproject\myapp\node_modules\express\lib\router\index.js:190:10)

你能告诉我我做错了什么以及变量声明是如何工作的吗?

2 个答案:

答案 0 :(得分:2)

这是因为您的列表变量是从您想要使用它的scope中定义的。尝试在回调函数之外声明变量:

var listing;
 db.once('open', function (callback) {
    var toSchema = mongoose.Schema({
       name: String,
       state : Boolean
    });
    listing = mongoose.model('todoSchema', toSchema);
 });

 // you can use it anywhere as soon as it is initialized in time.

答案 1 :(得分:0)

混淆来自于JavaScript没有块范围,但它具有功能范围。您正在函数中定义listing,因此该变量仅存在于该函数中。尝试在函数范围之外定义变量:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var listing;

mongoose.connect('mongodb://localhost/test');

 var db = mongoose.connection;
 db.on('error', console.error.bind(console, 'connection error:'));
 db.once('open', function (callback) {
    var toSchema = mongoose.Schema({
       name: String,
       state : Boolean
    });
    listing = mongoose.model('todoSchema', toSchema)
 });

 /* GET home page. */
 router.get('/', function(req, res) {
   var silence = new listing({ name: 'Silence', state:true })
   console.log(silence.name) // 'Silence'
   res.render('index', { title: silence.name });
 });
module.exports = router;