toarray function dont updated outside the blocks - mongodb

I try to change an array of user in my mongodb from another collection.
this is the code:
phrases.find({albumID: tileid}).toArray(function(err, results) {
if (results.length > 0) {
for(var j = 0; j < results.length; j++)
{
for(var z = 0; z < o.Phrases.length; z++)
{
if(o.Phrases[z].id == results[j]._id)
{
o.Phrases.splice(z,1);
break;
}
}
}
}
});
console.log(o.Phrases);
.
.
.
after update the collection
When I do logs, I don't see the change being made.
If I do the logs in toArray blocks, I see the changes.
I read alot, And dont found solution only that toArray is async function but not how to change it or to use it.
Maybe there is another way to change the collection to the array.
thanks for helping.

Related

Dataset or Statistics Mode

I expected this question to have a really straightforward answer but can't seem to find anything in the documentation.
I need to be able to get the mode of a dataset or a statistics element. All I can find is min, max, mean and median.
Is there no function that gives the mode?
Thank you
I ended up using the below function, but I still find it weird that the mode function is not built-in...
int maxCount=0;
for(int i=0; i<QueueLengthDailyMaq.size(); ++i)
{
int count=0;
for(int j=0; j<QueueLengthDailyMaq.size(); ++j)
{
if(QueueLengthDailyMaq.getY(j) == QueueLengthDailyMaq.getY(i))
{
count++;
}
if(count > maxCount)
{
maxCount = count;
mode = QueueLengthDailyMaq.getY(i);
}
}
}

CS50 pset 3: Tideman sort_pairs function

I need some assistance in understanding the logic behind this function. This is my current sort_pairs function in Tideman:
// Sort pairs in decreasing order by the strength of victory
void sort_pairs(void)
{
qsort(pairs, pair_count, sizeof(pair), compare);
return;
}
// Function for sort_pairs
int compare(const void *a, const void *b)
{
const pair *p1 = (const pair *) a;
const pair *p2 = (const pair *) b;
if (p1->winner < p2->winner)
{
return -1;
}
else if (p1->winner > p2->winner)
{
return 1;
}
else
{
return 0;
}
}
This does not clear check50 and I looked online to find how to approach this problem. It seems that most functions compare the values from the preferences array instead (eg preferences[pairs[i].winner][pairs[i].loser]) . My previous functions vote, record_preferences, and add_pairs all clear check50. I have not advanced beyond sort_pairs yet.
Why can't I compare the strength of victory directly from the pairs array instead since I already have the data stored there?
You don't need to make this so complex, you can use your own sorting here. Let's try a simple insertion sort-
void sort_pairs()
{
pair temp;
for (int i = 1, j; i < pair_count; i++)
{
temp = pairs[i];
j = i - 1;
for (; j >= 0 && preferences[pairs[j].winner][pairs[j].loser] < preferences[temp.winner][temp.loser]; j--)
{
pairs[j + 1] = pairs[j];
}
pairs[j + 1] = temp;
}
}
The pair struct looks like-
typedef struct
{
int winner;
int loser;
}
pair;
Explanation:-
We go through each pair of elements inside the pairs array - starting at 1 since I'm going to compare with the previous element (j = i - 1)
Now we check all the previous elements from the current element and compare them with the key - preferences[pairs[INDEX].winner][pairs[INDEX].loser]
This is the key you should be sorting by. preferences[WINNER_ID][LOSER_ID] means the amount of people that prefer WINNER_ID over LOSER_ID.
And that's pretty much it!, it's simply a insertion sort but the key is the important part.

How to fix : The object of type 'GameObject' has been destroyed but you are still trying to access it ? -Unity

I made a 5 second time bar to replace the wave. when wave1 has been 5 seconds it will move to wave2. then the first wave will be destroyed. when I got to wave3, an error came out. here's my code:
IEnumerator ChangeWave()
{
for (int i = 0; i < wave.Length - 1; i++)
{
yield return new WaitForSeconds(5f);
Destroy(wave[i]);
wave[i+1].SetActive(true);
}
}
the error said The object of type 'GameObject' has been destroyed but you are still trying to access it. - unity
sorry for my bad english.
There's a few things going on here.
Remove the -1 in the for loop, so it iterates to end
wave[i+1] will cause an error, so check if (i < wave.Length -1)
Destroy( array[index] ) will try and access Game Object but it's destroyed, so you should create a temporary var to hold gameobject, and set null to the array element.
Some fixes below - there might be a better way, but this is what I use:
Version 1 - Using the above fixes
IEnumerator ChangeWaveV2()
{
for (int i = 0; i < wave.Length; i++)
{
yield return new WaitForSeconds(.2f);
var t = wave[i];
wave[i] = null;
Destroy(t);
if(i < wave.Length - 1)
wave[i + 1].SetActive(true);
}
}
Version 2 - A variation without needing to null out the element and create a temporary var. If you iterate from end to front of the array, you can freely Destroy() GameObjects in an array. May not be useful as it changes the wave array.
IEnumerator ChangeWaveV3()
{
System.Array.Reverse(wave);
for (int i = wave.Length - 1; i >= 0; i--)
{
yield return new WaitForSeconds(.2f);
Destroy(wave[i]);
if(i > 0)
wave[i - 1].SetActive(true);
}
}

ID element to not display

I have this code:
function showForm()
{
var a=document.getElementById("opts").value;
if(a==1)
{
document.getElementById("f1").style.display="block";
document.getElementById("f2").style.display="none";
document.getElementById("f3").style.display="none";
document.getElementById("f4").style.display="none";
document.getElementById("f5").style.display="none";
document.getElementById("f6").style.display="none";
document.getElementById("f7").style.display="none";
document.getElementById("f8").style.display="none";
document.getElementById("f9").style.display="none";
document.getElementById("f10").style.display="none";
document.getElementById("f11").style.display="none";
document.getElementById("f12").style.display="none";
document.getElementById("f13").style.display="none";
document.getElementById("f14").style.display="none";
document.getElementById("f15").style.display="none";
document.getElementById("f16").style.display="none";
document.getElementById("f17").style.display="none";
document.getElementById("f18").style.display="none";
document.getElementById("f19").style.display="none";
document.getElementById("f20").style.display="none";
document.getElementById("f21").style.display="none";
document.getElementById("f22").style.display="none";
document.getElementById("f23").style.display="none";
document.getElementById("f24").style.display="none";
document.getElementById("f25").style.display="none";
document.getElementById("f26").style.display="none";
document.getElementById("f27").style.display="none";
document.getElementById("f28").style.display="none";
document.getElementById("f29").style.display="none";
document.getElementById("f30").style.display="none";
document.getElementById("f31").style.display="none";
document.getElementById("f32").style.display="none"
}
if(a==2)
{
//...
}
}
Is it possible to get this js to be smaller?
I use it for the website www.borrani.com in the double dropdown selector
A loop could certainly make it smaller:
for (var i = 1; i <= 32; i++) {
document.getElementById('f' + i).style.display = 'none';
}
document.getElementById('f1').style.display = 'block';
You might even try querySelectorAll to explicitly identify your elements based on a pattern, assuming you can define a unique pattern for only the elements you want:
var elements = document.querySelectorAll('[id^="f"]');
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
document.getElementById('f1').style.display = 'block';
It may be slightly more code, but it de-couples the code from the specific number of elements being identified.

Template.templatename.rendered can't return any data from mongodb

I would like to know how can I get the data from mongodb in Template.templatename.rendered function. I tried click event on other template and it's all working fine and return the results i wanted. But what I need is to rendered the chart on load. But I could not get any data from the mongodb.
//poll.js
var drawPollChart = function(){
//returns data on other template methods except for
//Template.templatename.rendered
var dist = getDistinctQuestionId();
alert('dist:'+dist);
var data_x =[];
for(var i=0; i< 1; i++)
{
var count = getDataCount(dist[i]);
var uniq = getDistinctResponseBucket(dist[i]);
for(var j=0; j<uniq.length; j++)
{
//alert('data:' + count[uniq[j]] + ", label:" + uniq[j]);
data_x.push({
data : count[uniq[j]],
label: uniq[j]
});
}
}
Template.pollChart.rendered = function() {
//can't draw a thing cause can't get any data from mongodb
drawPollChart();
};
Help please? Thanks in advance.
Just because the template is rendered doesn't mean the DB is connected.
Use Meteor.status().status to detect the state of the connection. For example you could wait to render the pollChart-template at all until Meteor.status().status === 'connected'.