javascript - Cannot figure out what I am doing wrong with add friend button -
so trying figure out how make add friend button. using node.js route post jade view engine showing input button in form add contact button. using jquery change button name add contact contact requested on click, , added value addcontact: boolean type mongoose user schema.
here node.js app.js file:
app.post('/addcontact', user.addcontactpost);
here node.js route file:
exports.addcontactpost = function(req, res, err) { user.findbyidandupdate(req.signedcookies.userid,{ addcontact: req.body.addcontact }, function(err) { if(err) { console.log("post2"); return res.render('addcontacterror', {title: 'weblio'}); } }); };
here jade file:
extends layout block content div legend search results div#userresults user in ufirstname a(href='/user/#{user.id}') p #{user.firstname} #{user.lastname} form(method="post", action="/addcontact") input(type='submit', id='addcontact', value='add contact').addcontact
here jquery script being called:
$('.addcontact').click(function() { if($(this).value!=='contact requested') { return $(this).value('contact requested'); } });
here mongoose user schema, also, tried make friend schema not sure how go currently.
var mongoose = require('mongoose'), schema = mongoose.schema, bcrypt = require('bcrypt-nodejs'), salt_work_factor = 10; var userschema = new schema({ email: { type: string, required: true, lowercase:true, index: { unique: true } }, password: { type: string, required: true }, firstname: {type: string, required: true}, lastname: {type: string, required: true}, phone: {type: number, required: true}, birthday: {type: date, required: true}, addcontact: {type: boolean} }); userschema.pre('save', function(next) { var user = this; console.log("email exists"); // hash password if has been modified (or new) if (!user.ismodified('password')) return next(); // generate salt bcrypt.gensalt(salt_work_factor, function(err, salt) { if (err) return next(err); // hash password along our new salt bcrypt.hash(user.password, salt, null, function(err, hash) { if (err) return next(err); // override cleartext password hashed 1 user.password = hash; next(); }); }); }); userschema.methods.comparepassword = function(candidatepassword, cb) { bcrypt.compare(candidatepassword, this.password, function(err, ismatch) { if (err) return cb(err); cb(null, ismatch); }); }; module.exports = mongoose.model('user', userschema);
here friend schema tried use gives me error due user schema:
/*var mongoose = require('mongoose'), schema = mongoose.schema, objectid = schema.objectid, userschema = require('./user').userschema; var friendrequest = new schema({ from: {type: objectid, index: true}, to: {type: objectid, index: true}, confirmed: {type: boolean, index: true}, created: {type: data, default: date.now} }); // friendrequest.pre('save', function youcantbefriendyourself(next) { if (this.from === this.to) { next(new error("a user can't befriend themselves.")); } else { next(); } }); friendrequest.pre('save', function lookforpendingrequest(next, done) { next(); var query = { $or: [ {'from': this.from,'to' : this.to}, {'from': this.to,'to' : this.from} ] }; mongoose.model('friendrequest').find(query, function(err,res) { if (res.length > 0) done(new error("pending friendrequest exists already")); else done(); }); }); // @untested friendrequest.pre('save', function seeifalreadyfriends(next, done) { next(); var query = { "_id" : this.from, "friends" : this.to }; mongoose.model('user').find(query, function(err,res) { if (res.length > 0) done(new error("the users friends")); else done(); }); }); module.exports.friendrequest = mongoose.model('friendrequest', friendrequest); module.exports.friendrequestschema = friendrequest;
any awesome.
Comments
Post a Comment