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.
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.
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.
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:
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:
@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.
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.
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.
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âŚ
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.
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