5

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

数据库.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()}

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

有人可以帮我解决吗?

4

4 回答 4

2

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

于 2013-03-22T16:04:39.263 回答
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';
于 2013-12-03T17:32:37.480 回答
1

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

在 package.json: 而不是 "mongoose": "2.6.5" 使它成为 "mongoose": "*"

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

输入“npm uninstall mongoose”,然后输入“npm install mongoose”

似乎 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/

于 2016-02-04T17:23:43.260 回答
0

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

于 2014-12-21T04:50:24.123 回答