Match.Where on check function called two times, why?


#1

The function on Match.Where is called two times, why?
Is normal or maybe a bug ?

checkFn = Match.Where(function(x) {
  console.log('checkFn');
  return true;
})

check('', checkFn);

output:

=> App running at: http://localhost:3000/
I20151107-17:19:27.946(1)? checkFn
I20151107-17:19:27.946(1)? checkFn

#2

I create a meteor issue: https://github.com/meteor/meteor/issues/5630
here.


#3

Well, where did you copy this code from? What file in your project?
I think that’s not a Meteor issue, just a misunderstanding on how Meteor deal with the files.


#4

Hi @borges
is not a production file. Just a bug reproducction.
Create a clean project, create a file on root project with this code line and test your self.


#5

But that’s the problem: you can’t run this piece of code on a root file on Meteor.
You probably want to have the checkFn function on that “root file” (a file in a common directory, that will be copied to both client and server) and use check('', checkFn) inside a Meteor Method or Template event.

Code that runs on “root files” is no reliable to run once. If you really want that to run a single time, you should move it to a Meteor.startup() block.

And if that works for you, I suggest you to close your issue on github.


#6

Meteor doc not say nothing about use check on Methods or template only. I think it can be use everywhere.
I recreate the example again, a little different and it confirm that checkFn is called twice what is a wrong behaviour.
With this example:

checkFn = Match.Where(function(x) {
    console.log('checkFn');
    return true;
});

function callmeOne() {
  console.log('start callmeOne');

  check(true, checkFn);
  console.log('end callmeOne');
}

callmeOne();

and the output is:

=> App running at: http://localhost:3000/

I20151108-20:39:28.198(1)? start callmeOne
I20151108-20:39:28.199(1)? checkFn
I20151108-20:39:28.199(1)? checkFn
I20151108-20:39:28.200(1)? end callmeOne

The function containing the check call is called one time but check is called twice.


#7

You’re not getting it. That’s the correct behaviour.

Meteor apps, unlike Node projects, shouldn’t have functions or code on any “root file”. That’s not how it works. If you do this, it’s not guaranteed how many times it would run.

Meteor merges and wraps everything that’s on “root files” on javascript’s immediate functions. However, this immediate function can be called many times, that’s why your code is called twice.

If you want to check an object, you want to do it, for instance, on the client, after you received user’s input, right? So, you can:

checkFn = Match.Where(function(x) {
    console.log('checkFn');
    return true;
});

if (Meteor.isClient) {
    Template.hello.events({
        'submit #form': function (e) {
            check(e.target.value, checkFn);
        }
    });
}

#8

@borges. Thanks for take the time answer me but your are wrong.

First when I say “root file” I mean a file on meteor project root folder not inside any folder.
I don´t think that meteor not guaranteed how many time a function is called.
You can use check on every place: server or client code. Is like put a
if condition and throw an Error is condition fail but encapsulate on a convenient API.
Check is for break code execution is value not passed pattern validation.

I you test your own code example, when you click submit form and check get passed, you will get the console output ‘checkFn’, twice.

Thanks anyway.


#9

Now I get what you said. However, you still haven’t understood what I meant.

Anyway, what’s the matter with check being called twice? If it throws an error or don’t only the logic break matters. It’s not like you’ll be calling console.log there.

Yet, it could be a library error.


#10

Matter cause any piece of code can´t be execute more time that is design for.
You can use console.log or any log system you need to track what and where things happend and Match.Where is not different than any other Meteor function.
I you have the same twice behaviour like me, this confirm check package bug and we need MDG accept it and fix on next release.
I had reproduce too with Match.test function and Match.Where is runned twice there too.

I belive that this bug is important for all of us that use check or Match.test function.

For anyone that need track this issue, https://github.com/meteor/meteor/issues/5630


#11

Hey can you create a reproduction repo, so people from here or MDG will know what exactly you’re talking.

I assume you’ll get response from MDG saying “please submit reproduction of the issue in a repo”, it will be best if you do that even before they are ask. It’ll be helpful if community or MDG want to help you.

NOTE I’m also worried bcz I’m using Match package in production too…


#12

I can make a repo if MDG or anyone here needed but I suppose is matter of copy paste the example lines above. More faster than clone a github repo.

I don´t know is a simple check like, Ex:

check(str, String);

is runned twice too. I only tested with Match.Where functions.


#14

So, I’ve tried my hardest to reproduce this (copied your code into a fresh project) and can’t.

The closest I can get is a log in the browser (client) and one on the console (server) if I put the code in a shared location. Otherwise I get one logged line only.

What OS and Meteor version are you on?


#15

I clean meteor and install it again. Remove ~\.meteor is the correct way to doit??

I create a repo for bug reproduction, is here: https://github.com/bySabi/twice-match-where-call

The Match.Where function is called twice on all use cases: client, server, and both.

I have latest meteor 1.2.1 on OSX.

Maybe is a problem on my environment.
What is the recommended way to clean and start from scratch?


#17

Your repo has three calls - one from the lib/ folder (which will execute on the client and the server), one in the client/ folder (which will execute on the client only) and one in the server/ folder (which will execute on the server only).

So, you end up with:

called from     |   logged on client   |   logged on server
----------------+----------------------+------------------
lib/            |  yes                 |   yes
client/         |  yes                 | 
server/         |                      |   yes

And there’s your two logs on the client and two on the server


#18

I thought that at first too @robfallows

but if you look more carefully at the repo, or even run it as I did, you realise @bysabi differentiates the text in console.log() for each file

If you comment out the entire contents of ‘/lib/file,js’ so that on the client it is only called once, the result is still

start callmeOne _ client only
checkFn _ client only
checkFn _ client only
end callmeOne _ client only


#19

It seens @garrilla have the same wrong behaviour than me. Then is not related to my environment but a Meteor bug and we need track github issue: https://github.com/meteor/meteor/issues/5630

Thanks for take the time to test it.


#20

You’re right of course. My bad.

However, I have got this to behave (pre 1.2.1). So, it looks like a new introduction. I’m just trying to bottom out what’s causing it.