2015-10-26 27 views
1

我想從我的signin.ejs文件登錄,點擊在 我的登錄頁面的登錄按鈕後,它顯示「無法讀取屬性集合未定義」什麼是 問題。連接到node.js中的mongodb時,無法讀取未定義的屬性「集合」?

我這樣定義我的路線app.js:

app.post('/login', routes.dologin); 

我定義的index.js我dologin路線:

exports.dologin = function (req, res) { 
res.locals.session = req.session; 

var user = req.body.user; 
db.authenticateUser(user.email, user.password, function  (err, response) { 
if (err) { 
....... 

....... 
} else { 
....... 

........ 
} 
}); 
}; 

在我db.js:

var mongo = require('mongoskin'), 
crypto = require('crypto'); 

module.exports = function (config) { 

var USERS_COLLECTION = 'users', 
ORDERS_COLLECTION = 'orders', 
salt = 'supersecretkey', 
db; 

    authenticateUser: function (emailId, password,   callback) { 
    db.collection(USERS_COLLECTION).count({email : emailId,   password: encryptPassword(password)}, function (err,   count) { 
    if (err) { 
console.log("error authenticating user: " + err); 
callback(new Error(err)); 
} else if (count === 0) { 
callback(new Error("emailid/password did not match")); 
} else { 
callback(null); 
} 
}); 
}, 
    } 

獲取「Collection undefined」的問題在哪裏?我想在這裏 一切都是正確的...這裏有什麼問題嗎?告訴我..請謝謝。

+0

'db'是不確定的,'VAR db'不是有效的數據庫對象 –

回答

0

您應該添加以下代碼db = mongo.db('localhost:27017/yourdb');

var mongo = require('mongoskin'), 
    crypto = require('crypto'); 

module.exports = function (config) { 

    var USERS_COLLECTION = 'users', 
     ORDERS_COLLECTION = 'orders', 
     salt = 'supersecretkey', 
     db = mongo.db('localhost:27017/yourdb'); 

    authenticateUser: function (emailId, password, callback) { 

     db.collection(USERS_COLLECTION).count({ 
      email: emailId, 
      password: encryptPassword(password) 
     }, function (err, count) { 
      if (err) { 
       console.log("error authenticating user: " + err); 
       callback(new Error(err)); 
      } else if (count === 0) { 
       callback(new Error("emailid/password did not match")); 
      } else { 
       callback(null); 
      } 
     }); 
    }, 
} 
+0

您好感謝這是工作。 – Nag

+0

但我加入這樣的回報聲明: \t回{ \t \t配置:功能(配置){ \t 分貝= mongo.db((配置配置: 'mongodb的://本地主機:27017 /貝寶' ),{w:1}); \t \t} – Nag

+0

但它不工作爲什麼?你能告訴我嗎? – Nag

相關問題