简体   繁体   English

未定义的承诺,而不是布尔值

[英]Promise Undefined instead of boolean

I'm using Promise library to get result of another promise-request library with cheerio request but instead of boolean I keep getting undefined 我正在使用Promise库来获取另一个带有cheerio请求的promise-request库的结果,但是我undefined得到布尔值,而是一直保持undefined

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

and my method with promise-request 和我的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');
    });
};

Should I promisifyAll libraries? 我应该promisifyAll图书馆吗?

I guess what you actually want is 我想你真正想要的是

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

That will return the boolean from the promise callback so that it becomes the fulfillment value. 这将从promise回调return布尔值,使其成为实现值。

You need to change this section 您需要更改此部分

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');
    });

If you change your code to this it should work. 如果将代码更改为此,它应该可以工作。

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');
    });

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

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