I'm facing a challenge with my code. When I execute this specific code snippet in the Chrome console, it works fine as intended. However, when the same code runs in the application, it doesn't perform as expected.
The purpose of this code is to compare the elements in the selectedItemIDSet
array with the elements in the TotalOfallIds
array. If there's a match, it should display a "Yes" message, otherwise a "No" message.
var selectedItemIDSet = Session.get('selectedItemIDSet');
console.log(selectedItemIDSet);
The console.log() output for the above code snippet is:
["GttzCQLzMeqj", "yzML7ZLvkWkjBBvq5", "2HwJhHyjDhDogJK"]
var TotalOfallIds = buyList.find().fetch().map(function(u) { return u._id; });
console.log (TotalOfallIds);
The console.log() output for this snippet is:
EkebEek7KH8mdpNXg,LZJKA8S3wYNwHakzE,ikrbCDuttHrwkEcuv, yzML7ZLvkWkjBBvq5,e72HwJhHyjDhDogJK,GttzCQLzMeqjJP4Ae, XzGxS3LDtJ4DrXSdc,FJDhf7mzCxX4CKThe,TaTg6hf3Gok5NsWYp, NhDXRMnLWpwfAkWzJ,nG9TsoScYGFbEuerr,8CdapJoeSxSScHKFs, EsjSJfSgatrYTEnDK
var noOfloops = selectedItemIDSet.length;
var selectedItemId = this._id;
var allclicked;
for(var i = 0; i < noOfloops; i++ ) {
var clickedImg = selectedItemIDSet[i];
allclicked = TotalOfallIds.indexOf(clickedImg) > -1;
console.log("May: " +allclicked);
if (allclicked == 1 ) {
alert("Yes! " );
// return "selectedItem";
}
else {
alert("No! ");
//return "";
}}
Although it functions correctly in the console, the issue arises in the template helper where the loop seems to iterate (three times) over the selectedItemIDSet
array and (13 times) over all properties of TotalOfallIds
. Additionally, it only checks the GttzCQLzMeqj
property against TotalOfallIds
!
This inconsistency is perplexing to me!
Below is the code from my template helper:
'selectedItem' : function () {
var selectedItemIDSet = Session.get('selectedItemIDSet');
var TotalOfallIds = buyList.find().fetch().map(function(u) { return u._id; });
var loops = selectedItemIDSet.length;
var selectedItemId = this._id;
var allclicked = true;
for(var i = 0; i < loops; i++ ) {
alert("Checking..." + selectedItemIDSet[i]+ " in " +TotalOfallIds);
var clickedImg = selectedItemIDSet[i];
allclicked = TotalOfallIds.indexOf(clickedImg) > -1;
if (allclicked == 1 ) {
alert("Yeeeeiiiii! " );
return "selectedItem";
}
else {
alert("Nnoooooo! ");
return "";
}
}
},
Apologies for the lengthy description!
Any assistance would be highly appreciated.