find failures when using promises

36 views
Skip to first unread message

Mark Edwards

unread,
Apr 18, 2015, 12:52:23 PM4/18/15
to mongoo...@googlegroups.com
tearing my hair out here... fortunately I still have a few follicles left.

I have an app that requires some update operations to be sequential, so I am using the promise API's to attempt to achieve this.

Unfortunately, I'm experiencing some query failures where using the .exec() suffix simply results in the results array becoming undefined.

When I run this query outside of the promise structure, I'm seeing results, but not when it's within the promise structure.

I am constrained to using the promise structure to avoid race conditions.
This is my methodology in the code...

var pFreqDeliver=goose.frequency                              
.find({'block':data.block})
.exec(function(e,a) {
 console
.log('inside pFreqDeliver...');
  console
.log(a);
 
var resp={'block':data.block,'content':a};
  socket
.emit("rxfrequpdate",resp)
});



var pDoIt=pTalent.then(function(){console.log('pTalent Done');})
                               
.chain(pOutput)
                               
.then(function(){console.log('pOutput Done');})
                               
.chain(pFreq)
                               
.then(function(){console.log('pFreq Done');})
                               
.chain(pFreqUpdate)
                               
.then(function(){console.log('pFreqUpdate Done');})
                               
.chain(pCommit)
                               
.then(function(){console.log('pCommit Done');})
                               
.chain(pFreqDeliver)
                               
.then(function(freqs){
                                        console
.log(freqs);
                                        console
.log('pFreqDeliver Done');
                               
});
pDoIt
.fulfill();


I have only shown the problematic promise... pFreqDeliver.

Whereas there is heaps of data in this collection - the console log shows undefined upon return.

If I change this to a find(<conditions>,<callback>) pattern, I can see data, but the promise structure breaks.

Any ideas?


Thanks in advance...

Mark...





Jason Crawford

unread,
Apr 18, 2015, 2:00:59 PM4/18/15
to mongoo...@googlegroups.com
I really don't know, but a year or two ago I had a different problem with Mongoose promises (they weren't playing nicely with chai-as-promised: https://github.com/domenic/chai-as-promised/issues/30), and so I started using Q promises instead, like this:

var Q = require('q');

mongoose.Query.prototype.qexec = function () {
  return Q.npost(this, 'exec', arguments);
}

Now I use qexec() instead of exec() and everything works well. You could try that and see if it helps.

Good luck,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Edwards

unread,
Apr 18, 2015, 8:07:05 PM4/18/15
to mongoo...@googlegroups.com
Jason, thanks for this. Yeah googling around about goose and promises did mention a few posts about "q". You'll have to forgive - I'm very much a newbie who has simply jumped into the deep end of async paradigms, node js, mongo and goose - still very much learning the ropes. Promises could be a saviour to ensure everything happens in the right order, just gotta get the right model to work!

Cheers mate.

m

You received this message because you are subscribed to a topic in the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-orm/z8BVQViSGCA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongoose-orm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

regards,

Mark P. Edwards

0408601107
 
 
 
 
 
 
 

 
--------------------------------------
Get 2GB Free online storage @ dropbox


Jason Crawford

unread,
Apr 19, 2015, 11:55:12 AM4/19/15
to mongoo...@googlegroups.com
No need to apologize! I've had a good experience overall with promises, see what I wrote here: https://www.quora.com/Should-I-learn-and-use-the-promise-pattern-in-JavaScript/answer/Jason-Crawford

But not all promise implementations behave exactly the same way, and the ones built into Mongoose weren't working with chai-as-promised, so maybe there's something a little odd about them.

Best of luck,
Jason

Reply all
Reply to author
Forward
0 new messages