运行node应用时出现未捕获的、未指定的“错误”

5

我是一个刚开始学习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);
    }
  });
}

当我运行node app时,它报告了以下错误。
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');
  });  
});

有人能帮我解决这个问题吗?


有些代码丢失了,你给 addUser 函数的回调函数是什么样子的?另外,也许你不应该在这里放置你的用户名和密码。 - Johanna Larsson
嗨,我已经编辑了你的用户名/密码和地址 - 发布这种信息并不是一个好主意,除非你想让别人访问你的Mongo Cloud数据库? - Rich Andrews
谢谢Rich。我确实想在编辑后删除用户名和密码,但忘记了 :-( - Sripaul
@ErikKronberg已将我的代码包含在app.js中。 - Sripaul
提示:mongoose有连接事件,您可以挂钩来调试这些问题。 - Pier-Luc Gendreau
4个回答

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

看起来在MongoLab中出现了无法连接到Amazon云的问题。我尝试使用Joyent Cloud,它可以正常工作。


1

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接