我是MongoDB的新手,拥有一个用户数据库。我正在尝试这样,用户可以点击他们喜欢的鞋子的按钮,它更新数据库。我的架构如下:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
// define the schema for our user model
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
});
// methods ======================
// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);
发布数据库功能:
exports.addToFavs = function(req, res) {
var size_result = req.query.size;
var brandToFind = req.query.brandToFind;
req.db.users.update({email: req.user.email}, {$set: favoriteShoes[{name: brandToFind, size: size_result}]}); /*, function(error, favoriteShoes) {
if (error) return next(error);
if (!favoriteShoe) return next(new Error('Failed to save')); */
console.info('Added %s with id=%s', brandToFind, size_result);
res.redirect('/favorites');
//})
};
HTML按钮:
<form action="/result" method="post">
<center><button>Save to Favorites</button></center>
</form>
适当的路线:
app.post('/result', searchResults.addToFavs);
我真的很困惑如何让它工作......不知何故它无法通过db表“users”读取(通过控制台验证这是表的名称)所以试图让它工作,但它不起作用......