I'm trying to build out a system so that the default accounts sytem built into MeteorJS will have a friends list. Unfortunately, this just isn't working, and I can't figure out why. I'm currently attempting to simply add a friendList array to the user.profile, and then simply append the added user to the end of that list.
Here's the code:
Server:
Meteor.methods({
addFriend: function(friendUsername){
friendUserId = Meteor.users.findOne({username:friendUsername});
if(friendUserId){
Meteor.users.update({_id:Meteor.userId()}, {$set:{"profile.friendList":Meteor.user().profile['friendList'].push(friendUserId)}});
return 1;
}
else return 0;
}
});
Client:
Template.addFriendPage.events({
"keypress .addFriendByUsername": function(event, template){
if(event.which == 13){
event.preventDefault();
var user = Meteor.userId();
var friendUsername = template.find(".addFriendByUsername").value;
console.log(friendUsername);
console.log(user);
console.log(Meteor.call("addFriend",friendUsername));
if(Meteor.call("addFriend", friendUsername)){
alert("successfully added friend!");
template.find(".addFriendByUsername").value = "";
} else alert("not a valid username");
}
}
});
The html is just a template that I can put a username into. And I know I have to add in the part where I add the current user to the other user's friend list, but I want to get this working first.
Your main problem is you're setting a push. Instead, you need to use $push.
var userId = Meteor.user();
Meteor.users.update(userId, {$push:'profile.friendList':friendUserId});
Second, I'd consider it a moderately bad idea to store friends on the user profile. Consider creating a friends collection.
Related
On my website there are users and guests, and I want both to be able to "like" elements on my website. For the users it’s not a problem to check if they already "liked" a specific element to prevent them from giving another "like" to the same element, but I am not sure about how to check if the guest "liked" an element to prevent multiple likes from one specific guest. I am using Angular and I thought about saving such info with cookies, but I don’t know how yet, and if it will be trusted enough or not. Any ideas on how to implement them? Thanks.
Best way use localStorage , to be more complex use Hush (value and key ) in localStorage .
HTML
<button type="button" (click)="liked()">
<span *ngIf="!unlike">like</span>
<span *ngIf="unlike">unlike</span>
{{like}}
</button>
TS
like=99
unlike:Boolean=true
liked(){
if(localStorage.getItem('like') == null){
localStorage.setItem('like','true')
this.like=this.like+1;
this.unlike =!this.unlike
}else{
localStorage.removeItem('like')
this.like=this.like-1
this.unlike =!this.unlike
}
}
like=99;
ipActionLike =['1','2'] ;
unlike:Boolean=true;
liked(){
//get information browser user ip
if(localStorage.getItem('like') == null){
for (var i = 0; i <this.ipActionLike.length; i++) {
if (this.itemArray[i] == ipUser) {
//remove ipUser from array
localStorage.removeItem('like');
this.like=this.like-1;
this.unlike =!this.unlike; }
else{
//push ipUser to array
localStorage.setItem('like','true')
this.like=this.like+1;
this.unlike =!this.unlike }
}
}
}else{
//push ipUser to array
localStorage.removeItem('like')
this.like=this.like-1
this.unlike =!this.unlike
}
}
Hope this helps.
I want to make a small invite system (User send an email to the friend with an ivitation code --> friend clicks on the public website everyone can go --> puts his invitation code in the text field and meteor searches for this code if it can find the code all is fine and he can continue but when meteor cant find the code he is one random internet user and he shouldnt can continue
So I need something which compares the inputted value with the data in the collection
this is my js file maybe some good things are already inside of it ;)
Template.Invite.onCreated(function() {
this.subscribe('invites');
});
Template.Invite.events({
'submit .Invite'(event) {
event.preventDefault();
var Invite = event.target.Invite.value;
}
});
Template.Invite.helpers({
results: function(){
return Invites.find({
code: Session.get('Invite'),
if (Invite = Invite)
{
FlowRouter.go('/');
}
});
}
});
my publish part in the main.js
Meteor.publish("invites", function() {
return Invites.find();
})
and the not important html
<template name="Invite">
<form class="Invite" >
<input type="text" name="Invite" placeholder="Invite Code" />
<input type="submit" value="Bestätigen" />
</form>
</template>
the insert in the Invite Collection works already but not the find and the compare
Thank you for your time and help ;)
I've created an invitation system a few times and this is how I did it.
When the user sends an invitation, you create a new document in the Invitation collection like this:
import { Random } from 'meteor/random';
const code = Random.hexString(16);
invitation = {
'code': code,
'sentTo': 'user#website.com',
'accepted': false,
}
Then when a user tries to sign up you need to call a method that grabs their invitation code and compares it to the code in the database
Meteor.methods({
'acceptInvitation'(code) {
check(code, String);
// find invitation in database
let invitation = Invitations.findOne({ 'code': code});
//check if invitation exists and if it hasn't already been accepted
if(invitation && invitation.accepted == false) {
//update invitation to now be accepted
Invitations.update(
{ '_id': invitation._id},
{ $set: { accepted: true }
);
return true;
} else {
throw new Meteor.Error('invalid', 'Your invitation code is not valid');
}
}
});
To make your invitation system even better, when you are sending the invitation email you can pass the invitation code as a parameter in the URL. Then when the user clicks the invitation link you can grab the code from the URL and automatically put it in the registration form for them. This prevents them making mistakes when they copy/paste it!
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
/*Getting the Running User Details*/
var result =
sforce.connection.query(
"SELECT Advisor__c " +
"FROM User " +
"WHERE Id = '{!$User.Id}'"
);
/*Grab the Records returned by executing the above SOQL Query*/
var userDetails = result.getArray("records")[0];
/*Initializing the Contact record for Update*/
var contactToUpdate = new sforce.SObject("Contact");
contactToUpdate.Id = "{!Contact.Id}";
/*If the Running User is an "Advisor" then set the
Contact With and associated fields*/
if(userDetails.Advisor__c === true){
contactToUpdate.Contact_With__c = "{!$User.Id}";
contactToUpdate.Last_Advisor_Touch__c = new Date();
}
/*If the Running User isn't an "Advisor" then set the
Contact With 2 and associated fields*/
else{
contactToUpdate.Contact_With_2__c = "{!$User.Id}";
contactToUpdate.Last_Non_Advisor_Touch__c = new Date();
}
var result = sforce.connection.update([contactToUpdate]);
if(result[0].success === true){
location.reload();
}
else{
alert(result[0].errors.message);
}
I added a custom check box field to our user profiles titled "Advisor" The code is suppose to distinguish which field to update based on if a user has this box checked or not. If yes update this field, If not update that field. Instead though it's returning a 'Unexpected Token ILLEGAL. Not sure why.
Seems like you have a special hidden character in following line
contactToUpdate.Last_Advisor_Touch__c = new Da te();
in word Date
If you just rewrite it it should work.
Specifically you have the infamous ZERO WIDTH SPACE between Da and te probably comes from this. To eliminate such things you can use this tool or this one
I have the code below and I need a little help in modifying the code output.
The code below is for a Codeigniter chat application where the user enters their message in the text box (#text) and the message is output, the message is saved in a MySQL database via PHP and then output to the textbox (#received).
I would like to modify the code to include the message sender and also show the messages with differentiation between the sender and recipient like google chat or Text message threads. The data output is via javascript and I am still a novice in the language.
This is the current chat thread output. Current Chat Output. For the full CodeIgniter project code, Click this link. Any help or pointers will be appreciated.
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var time = 0;
var updateTime = function(cb) {
$.getJSON("index.php/chat/time", function(data) {
cb(~~data);
});
};
var sendChat = function(message, cb) {
$.getJSON("index.php/chat/insert_chat?message=" + message, function(data) {
cb();
});
}
var addDataToReceived = function(arrayOfData) {
arrayOfData.forEach(function(data) {
$("#received").val($("#received").val() + "\n" + data[0]);
});
}
var getNewChats = function() {
$.getJSON("index.php/chat/get_chats?time=" + time, function(data) {
addDataToReceived(data);
// reset scroll height
setTimeout(function() {
$('#received').scrollTop($('#received')[0].scrollHeight);
}, 0);
time = data[data.length - 1][3];
});
}
// using JQUERY's ready method to know when all dom elements are rendered
$(document).ready(function() {
// set an on click on the button
$("form").submit(function(evt) {
evt.preventDefault();
var data = $("#text").val();
$("#text").val('');
// get the time if clicked via a ajax get query
sendChat(data, function() {
alert("dane");
});
});
setInterval(function() {
getNewChats(0);
}, 1500);
});
</script>
<textarea id="received" rows="10" cols="50"></textarea>
<form>
<input id="text" type="text" name="user">
<input type="submit" value="Send">
</form>
According to the link it looks like it is impossible to differentiate messages by sender. Your application simply doesn't know who has sent a message.
So, first of all, you have to add a field in DB to store information about a sender. Most likely you'll have to implement some sort of authentication in the application. After that you'll be able to make differentiation on the client side.
On the client side you have to change the #received textbox to a div or some other block element. Doing this you'll be able to apply CSS styles to individual messages. Than you will need to change your addDataToReceived function in this way:
var addDataToReceived = function (arrayOfData) {
arrayOfData.forEach(function (data) {
var message = $('<div>');
message.html(data[0]);
if (data[2] === username) {
// data[2] is a new element, it points to an author of the message
//
// username indentificates a current user
// you'll save it to a variable after authentication somehow
message.addClass('self-message');
} else {
message.addClass('other-message');
}
$("#received").append(message);
});
}
It highly depends on a concrete realization, but all in all something like that.
I am using a custom form to submit data to Google Spreadsheets. When user attempts to submit the form, it is validated (with jQuery) and if the validation is passed, the main input field is being modified with jQuery and then the submission continues.
The user inputs a URL and during submission the script cuts off unnecessary parts of the link and adds IP (that is also gathered with jQ) of the user to it. After the modification is completed, the form continues submission and in result, only the modified data is being sent to Google Spreadsheet.
It works pretty well with 99% of submissions but there are several users whose browsers do not execute the script properly; i.e.: the URL they input is not being modified during the submission and thus, wrong data (unmodified) is sent to the Spreadsheet. I am not completely sure, but the problem may be caused by Firefox (but I am not 100% sure that all the faulty submission come from Firefox, if you want I could research and confirm whether it is only Firefox's issue). I have asked some of them (users, whose browsers seem not to execute script) about addons/if they had turned JS off but they say they did not do anything special with their browsers.
I am posting whole js file below so you could check whether I did some errors that could cause problems. Also, the script is running here (link), you can inspect the code to see what other jQuery stuff I use.
$(document).ready(function() {
//Tabbiing page is being initialised, doesn't really matter, I think
$.ionTabs("#fppp_taby");
//Script that checks whether user has been redirected back after submission, that's not the main problematic part yet
if (window.location.search != 0) {
var query = window.location.search.substring(1);
ideo = query.split("=")[1];
if (isNaN(ideo) || (ideo.length < 3) || (ideo.length > 10)) {
$("form").html("Wystąpił błąd podczas wysyłania Twojego zgłoszenia. Powiadom mnie o tym poprzez prywatną wiadomość podając również obecną godzinę za pomocą <a class='uline' href='http://www.erepublik.com/pl/main/messages-compose/2417512'>tego linka</a>.");
} else {
$("form").html("Dziękujemy za zgłoszenie!<br /><br />Możesz teraz przeczytać któryś z artykułów epolskiej prasy - lista wartch uwagi znajduje się po lewej stronie!");
var poZglosz = "Dziękujemy za zgłoszenie!<br />Spodziewaj się chlebków i broni od któregoś z tych graczy: Kijek93, twatwaratwa, Gregoric bądź Zawa99";
$("#poZglos").html(poZglosz);
$("#poZglos").removeClass();
}
} else {
var query = 0;
}
//Some simple stuff
$("#wlaczirc").click(function() {
$(this).hide();
$("#irc").html("<iframe src='http://webchat.quakenet.org/?channels=fppp&uio=OT10cnVlJjExPTM0OQ1f'></iframe>");
$("#irc").show("slow");
$("#info_irc").show("slow");
});
//Some data
infoPodkr = "Wklej tutaj prawidłowy link do twojego profilu!";
inputLink = $("#ffpolelinku");
//Resetting validation info in input
$(inputLink).focus(function() {
if ($(this).hasClass("podkresl") || $(this).val() == infoPodkr) {
$(this).val("");
$(this).removeClass("podkresl");
}
});
//Gathering ip
$.getJSON("http://jsonip.com/",
function(data) {
ip = data.ip;
});
//MAIN PART (executed when user pressses 'submit')
$("form").submit(function() {
//Form is being hidden (display:none class is being added)
$('aside').addClass("hidden");
//Check whether input is empty or contains error message (infoPodkr = error message vlaue), if yes, add class 'podkresl'
if (($(inputLink).val() == "") || ($(inputLink).val() == infoPodkr)) {
$(inputLink).addClass("podkresl");
$(inputLink).val(infoPodkr);
} else {
$(inputLink).removeClass("podkresl");
}
//Tesing whether user added proper link, if not, 'podkresl' is added
//if (!/\b(https?|ftp|file):\/\/[\-A-Za-z0-9+&##\/%?=~_|!:,.;]*[\-A-Za-z0-9+&##\/%=~_|]/.test(inputLink.val())){
if (!/http\:\/\/www\.erepublik\.com\/[a-z]{2}\/citizen\/profile\/\d{5,}$/i.test(inputLink.val())) {
$(inputLink).addClass("podkresl");
$(inputLink).val(infoPodkr);
}
//Split link, and get the last part of it ('wycId'), check if it's numeral, if not - add 'podkresl' class
podzielony = $(inputLink).val().split('/');
wycId = podzielony.pop();
if (isNaN(wycId)) {
$(inputLink).addClass("podkresl");
$(inputLink).val(infoPodkr);
}
//Check whether the input class 'podkresl' class (= something was wrong in some of the steps above). If yes, remove the 'hidden' class from 'aside' which holds the form and show it to the user. If there were no errors, add the IP to the 'wycId' and additional "-n" to the end of the value and put it into input. Then, submit form.
if ($(inputLink).hasClass("podkresl")) {
$('aside').removeClass();
return false;
} else {
if (ip !== '') {
$(inputLink).val(wycId + "-" + ip + "-n");
} else {
$(inputLink).val(wycId + "-0-n");
}
}
return true;
});
});