简体   繁体   English

猫鼬查找回调未执行

[英]Mongoose Find Callback Not Executing

I've been slowly converting a callback-heavy module to use promises (Q) and I'm hitting a strange issue with Mongoose Models running find() . 我一直在缓慢地将大量回调模块转换为使用Promise(Q),而Mongoose Models运行find()遇到了一个奇怪的问题。 In brief, the promise chain was never getting past the initial find command. 简而言之,promise链永远不会超越初始的find命令。 I've since reverted this method to its callback implementation and am still seeing no callback execution. 此后,我已将此方法恢复为其回调实现,但仍然看不到回调执行。 I've broken it down to its most basic behavior and the handleMedia function is never running. 我已将其分解为最基本的行为,并且handleMedia函数从未运行过。

function getMediaByURL(url) {
  Media.find({url: url}, handleMedia);

  function handleMedia(err, media) {
    console.log(err);
    console.log(media);
  }
}

The DB connection is being created in another module, but I'm seeing all of the right things initialize in the correct order, and the readyState of the Mongoose connection is 1 immediately before the find function is executed. 数据库连接是在另一个模块中创建的,但是我看到所有正确的事情都以正确的顺序进行了初始化,在执行find函数之前,Mongoose连接的readyState1 If I convert the find method to a promise using q.nbind as described here and log the value on a 1 second interval, the value is always {state: 'pending') . 如果我按照此处所述使用q.nbindfind方法转换为promise并以1秒的间隔记录该值,则该值始终为{state: 'pending')

I have another model that's saving over the same connection just fine with the promise version of find . 我有另一个模型可以通过find的promise版本保存相同的连接。 I'm at a loss. 我很茫然。

you declared "handleMedia function" after "find function" ,so only the callback function is not called.. in your code first find function is executed after that "handleMedia function" is initialized 您在“查找函数”之后声明了“ handleMedia函数”,因此仅在未调用代码的情况下才调用回调函数。在初始化“ handleMedia函数”之后,将首先执行查找函数

so try this 所以试试这个

function getMediaByURL(url) {
    function handleMedia(err, media) {
        console.log(err);
        console.log(media);
    }
    Media.find({url: url}, handleMedia);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM