运行节点应用程序时未捕获,未指定的“错误”

时间:2013-03-22 09:21:28

标签: node.js express mongoose

我是node.js express.js的初学者。 (今天早上开始:-))我已经将包含我的连接详细信息的db.js包含在mongolab和User.js中,这是我的模型。请找到以下代码。

Db.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports.mongoose = mongoose;
module.exports.Schema = Schema;

// Connect to cloud database
var username = "Ausername"
var password = "Apassword";
var address = 'Aaddress';
connect();

// Connect to mongo
function connect() {
  var url = 'mongodb://' + username + ':' + password + address;
  mongoose.connect(url);
}
function disconnect() {mongoose.disconnect()}

user.js的

var db = require('../lib/db');

var UserSchema = new db.Schema({
    username : {type: String, unique: true}
  , password : String
})

var MyUser = db.mongoose.model('User', UserSchema);

// Exports
module.exports.addUser = addUser;

// Add user to database
function addUser(username, password, callback) {
  var instance = new MyUser();
  instance.username = username;
  instance.password = password;
  instance.save(function (err) {
    if (err) {
      callback(err);
    }
    else {
      callback(null, instance);
    }
  });
}

当我运行节点应用程序时,它会报告以下错误

C:\Sripaul\Softwares\NodeJS\Projects\authentication>node app
Express server listening on port 3000

C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\lib\utils.js:413
        throw err;
          ^
Error: Uncaught, unspecified 'error' event.
    at NativeConnection.EventEmitter.emit (events.js:68:15)
    at Model.init (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\lib\model.js:554:31)
    at exports.tick (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\lib\utils.js:408:16)
    at Db.ensureIndex (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1066:28)
    at Db.indexInformation (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1200:28)
    at Cursor.toArray (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:124:30)
    at Cursor.each (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:166:32)
    at Cursor.nextObject.self.queryRun (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:441:39)
    at Cursor.close (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:687:5)
    at Cursor.nextObject.commandHandler (C:\Sripaul\Softwares\NodeJS\Projects\authentication\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:441:21)

app.js中的代码如下所示

app.post('/signup', function(req, res) {
  var username = req.body.username;
  var password = req.body.password;
  User.addUser(username, password, function(err, user) {
    if (err) throw err;
    res.redirect('/form');
  });  
});

有人可以帮我解决一下吗?

5 个答案:

答案 0 :(得分:2)

在mongo lab中看起来这个问题发生了,因为它无法连接到亚马逊云。我尝试使用Joyent Cloud,效果很好。

答案 1 :(得分:2)

面对类似问题,错误信息相同,但堆栈跟踪不同,

/%/node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
TypeError: Uncaught, unspecified "error" event.
    at TypeError (<anonymous>)
    at NativeConnection.EventEmitter.emit (events.js:74:15)
    at /%/authentication/node_modules/mongoose/lib/model.js:554:31
    at /%/authentication/node_modules/mongoose/lib/utils.js:408:16
    at /%/authentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1066:28
    at /%/authentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1200:28
    at /%/authentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:124:30
    at /%/authentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:166:32
    at /%/authentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:441:39

修复了它在db.js中的地址变量之前删除多余空格(如书中所示)。

var address = ' @dbh42.mongolab.com:27427/nockmarket';

应该是(介意空间)

var address = '@dbh42.mongolab.com:27427/nockmarket';

答案 2 :(得分:1)

我知道这是一个古老的话题,但我解决了这个问题:

package.json中的

: 而不是 &#34; mongoose&#34;:&#34; 2.6.5&#34; 做了 &#34; mongoose&#34;:&#34; *&#34;

然后在控制台(cd / home / yourusername / authentication)

键入&#34; npm卸载mongoose&#34; 然后 键入&#34; npm install mongoose&#34;

似乎mongoose 2.6.5是mongolab的弃用版本

了解更多信息:http://digiscape.co.uk/blog/tips/node/unspecified-error-event-not-authorized-for-query-on-db-system-indexes-at-model-js55431/

答案 3 :(得分:0)

我最初使用mongolab也遇到过这个问题。即使您有mongolab用户,您还必须在用户选项卡下创建数据库用户

答案 4 :(得分:-1)

好吧,我认为它更多是最新发布的问题,根据网站的说法,最新版本是版本3.我想这个版本现在支持所有免费计划,不再支持旧版本。

要解决此问题,请安装新驱动程序:在正常工作的目录中,运行命令:

$ npm install mongoose

使用$ node app重新启动应用后,一切都会好起来的。