So I am trying to figure out how to make an add friend button. I am using Node.JS to route a POST with Jade as my view engine showing an input button in a form that is just an add Contact button. Using Jquery to change the button name from Add Contact to Contact requested on click, and then added the value addContact: Boolean type for my mongoose User Schema.
Here is the node.js app.js file:
app.post('/addContact', user.addContactPost);
Here is the 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 is the Jade file:
extends layout
block content
div
legend Search Results
div#userResults
for 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 is the Jquery script being called:
$('.addContact').click(function() {
if($(this).value!=='Contact Requested') {
return $(this).value('Contact Requested');
}
});
Here is the mongoose User Schema, I actually also, tried to make a friend schema but not sure how to go about it 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");
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) return next();
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
// hash the password along with our new salt
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
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 is the friend schema I tried to use but it gives me an error due to the 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 are already friends"));
else
done();
});
});
module.exports.FriendRequest = mongoose.model('FriendRequest', FriendRequest);
module.exports.FriendRequestSchema = FriendRequest;
Any Help would be awesome.