So I am trying to have the add contact button disabled if the user.id already exists in the friendRequest array. However, for some reason the code is not working the way it is suppose to, and nothing is being disabled. Please help.
File for route:
exports.addContact = function(req, res, err) {
console.log('route add');
User.findById(req.signedCookies.userid, function(err, user) {
if (err) {
res.send(err);
} else {
if({ friendRequest: user.id, $exists: true }) {
return true;
console.log('route add true');
} else {
res.send(err);
}
}
})
};
exports.addContactPost = function(req, res, err) {
User.findByIdAndUpdate(req.signedCookies.userid, {
$push: {friendRequest: req.body.friendRequest}
}, function(err) {
if(err) {
console.log("post2");
console.log(err);
return console.log('error');
//return res.render('addContactError', {title: 'Weblio'});
}
else {
console.log('postsuccess');
//alert('Contact added');
res.json({response: true});
}
});
};
File for Script:
//see if friend exists if not friend request exist if not user sent request
$('.addContact').ready(function() {
$.get('/addContact',
{friendRequest: $(this).data('user')});
if($(this)===true) {
return $(this).attr('disabled', 'disabled').html('Contact Requested');
};
console.log(this);
});
//Add friends
$('.addContact').click(function() {
$.post('/addContact',
{friendRequest: $(this).data('user')});
if($(this).html!=='Contact Requested') {
return $(this).attr('disabled', 'disabled').html('Contact Requested');
}
});
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}
button.addContact(data-user=user.id) Add Contact
Currently the user gets added to the array and in my second script it turns the attribute of the button to disabled but I am not trying to do an ajax get to see if the user exists in the friendRequest array. The console.log('route add true') does not get called.