Meteor - no more callbacks for findOne

I am working on a Meteor project, and I have to say that this is not easy, especially for one thing: callbacks!

Everything is asynchronous, so I wonder how should I do to get the results from my mongodb.

var user = Meteor.users.findOne({username: "john"}); return (user); // sometimes returns "undefined" 

...

 var user = Meteor.users.findOne({username: "john"}); if (user) // so ok, I check if it exists! return (user); // Cool, I got my user! return (); // Ok and what should I return here? I want my user! 

I do not want to be dirty and set like setTimeout everywhere. Does anyone have a solution for this?


EDIT: I noticed in router.js with console.log that my data is returned 4 times. 2 times with the value undefined and 2 more times with the expected value. Apparently, it is still undefined. Why does the router go 4 times in this route? Does it display the first result of the return value in the router?

What should I return if find () did not find anything?


EDIT 2: Below is the code for understanding.

 this.route('profilePage', { path: 'profil/:_id?', waitOn: function() { return [ Meteor.subscribe('article', { prop: this.params._id}), // id can be id or username Meteor.subscribe('article', { userId: this.params._id}), // id can be id or username Meteor.subscribe('params'), Meteor.subscribe('profil', (this.params._id ? this.params._id : Meteor.userId())) ]; }, data: function() { if (this.params._id) { var user = Meteor.users.findOne(this.params._id); if (!user) user = Meteor.users.findOne({username: this.params._id}); console.log(user); return user; } else if (Meteor.userId()) return Meteor.user(); else Router.go("userCreate"); } }); 

I get this on the console: http://puu.sh/debdJ/69419911f7.png

(text version)

 undefined undefined Object_id: "o3mgLcechYTtHPELh"addresses: (....) Object_id: "o3mgLcechYTtHPELh"addresses: (....) 
+6
source share
3 answers

findOne(yourId) is a synchronization method equivalent to find({ _id: yourId}, callback) . The difference is that find() allows you to define a callback. If you do not pass the find() callback, this method will synchronize.

check wrapAsync: http://docs.meteor.com/#/full/meteor_wrapasync This allows you to sync -style coding using async operations.

Free EventedMind lesson: https://www.eventedmind.com/feed/meteor-meteor-wrapasync

+4
source

So far, my experience is that the Meteor Mongodb package consists in the fact that functions usually do not provide callbacks (for some reason, the insertion does ...), the functions are atomic (thus, synchronization).

There are meteor packets that async Mongodb can do if you want (I haven't tried anything).

I assume that this synchronous approach meets the simple goal of serving Mongodb. Thinking about this, one of my favorite Node scammers works with asynchronous callbacks / sockets, they create pain and create ... and hopefully this will make my code easier to read and understand and change ...

0
source
 var future = new Future(); var _h = Hunts.findOne({huntId}); if(_h) { future.return(_h) } else { return future.wait(); } 

on the /startup.js server you need: Future = Npm.require ("fiber / future");

0
source

Source: https://habr.com/ru/post/978957/


All Articles