2017-02-09 16 views
0

我使用的是無極庫來獲得與cheerio的請求的另一個諾言請求庫的結果,但不是布爾我不斷收到undefined無極未定義的,而不是布爾

return Promise.try(function() { 
     ..... 
    }).then(function() { 
     return self.checkGroupJoined(id); 
    }).then(function (data) { 
     console.log(data); 

,我的方法與promise-request

this.checkGroupJoined = function (steam_id) { 
    var options = { 
     uri: 'url', 
     transform: function (body) { 
      return cheerio.load(body); 
     } 
    }; 

    return rp(options).then(function ($) { 
     $('.maincontent').filter(function() { 
      if ($(this).find('a.linkTitle[href="url"]').length > 0){ 
       return true; 
      } else { 
       return false; 
      } 
     }); 
    }).catch(function (err) { 
     return error.throw('Failed to parse body from response'); 
    }); 
}; 

我應該promisifyAll庫嗎?

+1

你...沒有從'rp(options).then(function($){')返回任何東西。你只需選擇一些元素,將它們過濾到一個子集,然後...扔掉它 –

+0

so如何返回?因爲在識別它是否被發現之後返回? – Sandra

+0

你想要返回什麼?因爲現在你只是修改元素的集合...沒有什麼可返回的? –

回答

0

你需要如果你改變你的代碼,這一點,應該努力改變這一部分

return rp(options).then(function ($) { 
     // You are not returning anything here 
     $('.maincontent').filter(function() { 
      if ($(this).find('a.linkTitle[href="url"]').length > 0){ 
       return true; 
      } else { 
       return false; 
      } 
     }); 
    }).catch(function (err) { 
     return error.throw('Failed to parse body from response'); 
    }); 

return rp(options).then(function ($) { 
     let found = false; 
     $('.maincontent').filter(function() { 
      if ($(this).find('a.linkTitle[href="url"]').length > 0){ 
       found = true; 
      } 
     }); 
     return found; 
    }).catch(function (err) { 
     return error.throw('Failed to parse body from response'); 
    }); 
+0

是的,這是我錯過的東西,高五! – Sandra

+1

爲什麼這麼複雜?你爲什麼要用'filter'來循環(而不是每個')? – Bergi

3

我猜你真正想要的是

….then(function ($) { 
    return $('.maincontent').find('a.linkTitle[href="url"]').length > 0; 
}).… 

,使其成爲實現價值這將return自許回調布爾。