1
誰能告訴我如何將從MongoDB檢索的文檔轉換爲我自己創建的類?我不想用貓鼬,我使用github.com/christkv/mongodb-legacy v2.0.13將MongoDB文檔強制類型化
文檔的值從MongoDB的
{
id { MongoDb ID},
email: '[email protected]',
passwordHash: '$2a$04$Wjan4CloaZRYj60MGsDb6e7x11e1QYkjW3N2q5JYBDaKBNipLti36',
passwordSalt: '$2a$04$Wjan4CloaZRYj60MGsDb6e',
id: 'mrpmorris'
}
返回未能執行代碼
var user = Object.create(User.prototype, document)
拋出
TypeError: Property description must be an object: $2a$04$Wjan4CloaZRYj60MGsDb6e
at defineProperties (native)
at Function.create (native)
User類的異常
var assert = require('assert')
var bcrypt = require('bcrypt')
var User = (function() {
function User() {
this.passwordSalt = "hello"
}
User.prototype.constructor = User
User.prototype.setPassword = function (newPassword, callback) {
var self = this
assert.ok(newPassword != null, "newPassword cannot be null")
assert.ok(newPassword.length >= 8)
bcrypt.genSalt(2, function (err, salt) {
self.passwordSalt = salt
bcrypt.hash(newPassword, salt, function (err, hash) {
self.passwordHash = hash
callback(err);
})
});
}
User.prototype.checkPassword = function (password, callback) {
var self = this
bcrypt.hash(self.passwordHash, self.passwordSalt, function (err, hash) {
callback(err, hash === self.passwordHash)
})
}
return User
})()
exports.User = User
爲什麼不把用戶文檔作爲構造函數中的參數? – cbass