Home ยป Nodejs ยป Uncaught, unspecified 'error' while running node app

Uncaught, unspecified 'error' while running node app

Posted by: admin January 30, 2018 Leave a comment

Questions:

I am a beginner in node.js express.js. (started this morning ๐Ÿ™‚ ) I have included db.js which has my connection details to mongolab and User.js which is my model. Please find the code below.

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

And when i run node app, it reports the below error

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)

Code in in app.js is as below

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

Can someone please help me resolve it?

Answers:

Looks like in mongo lab this problem happened as it cannot connect to the amazon cloud. I tried with Joyent Cloud and it worked fine.

Questions:
Answers:

Faced the similar issue the error message was same however stack trace was different,

/%/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

fixed it be removing extra space(as in book) before the address variable in db.js .

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

should be (mind the space)

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

Questions:
Answers:

I know this is an old topic but I solved this problem like that:

in package.json:
rather than
“mongoose”: “2.6.5”
make it
“mongoose”: “*”

then in console (cd /home/yourusername/authentication)

type “npm uninstall mongoose”
then
type “npm install mongoose”

It seems mongoose 2.6.5 is deprecated version for mongolab

for more info: http://digiscape.co.uk/blog/tips/node/unspecified-error-event-not-authorized-for-query-on-db-system-indexes-at-model-js55431/

Questions:
Answers:

I also had this issue at first using mongolab. Even though you have a mongolab user, you also have to create a database user under the users tab

Questions:
Answers:

Ok, I think its more of a latest release issue, according to the website, the latest release is version 3. I suppose that all FREE plans are now supported on this version and old versions are no longer supported.

To solve this problem, install a new driver: in your working authenticated directory, run the command:

$ npm install mongoose

After restarting your app with $ node app everything should be fine.