Javascript 承诺未定义,而不是布尔值
我正在使用Promise库获取另一个Promise请求库与cheerio请求的结果,但我一直得到Javascript 承诺未定义,而不是布尔值,javascript,promise,bluebird,cheerio,Javascript,Promise,Bluebird,Cheerio,我正在使用Promise库获取另一个Promise请求库与cheerio请求的结果,但我一直得到未定义的 return Promise.try(function () { ..... }).then(function () { return self.checkGroupJoined(id); }).then(function (data) { console.log(data); 以及我的方法与承诺请求 this.checkGr
未定义的
return Promise.try(function () {
.....
}).then(function () {
return self.checkGroupJoined(id);
}).then(function (data) {
console.log(data);
以及我的方法与承诺请求
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
库吗?您需要更改此部分
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');
});
您需要更改此部分
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');
});
我猜你真正想要的是
….then(function ($) {
return $('.maincontent').find('a.linkTitle[href="url"]').length > 0;
}).…
这将返回promise回调中的布尔值,使其成为实现值。我猜您真正想要的是
….then(function ($) {
return $('.maincontent').find('a.linkTitle[href="url"]').length > 0;
}).…
这将返回承诺回调中的布尔值,使其成为实现值。您。。。没有从rp(选项)返回任何内容。然后(函数($){
。你只需选择一些元素,将它们过滤成一个子集,然后…扔掉。那么如何返回呢?因为我在确定是否找到它后返回。你想返回什么?因为现在你只是在装配一组元素…没有什么可返回的?你不需要因为我问有关pro的问题而否决我在这个地方编程,当人们感到卡住时,他们会不断地问关于编程的问题:)您好!您……不会从rp(选项)返回任何内容。然后(函数($){
。你只需选择一些元素,将它们过滤成一个子集,然后…扔掉。那么如何返回呢?因为我在确定是否找到它后返回。你想返回什么?因为现在你只是在装配一组元素…没有什么可返回的?你不需要因为我问有关pro的问题而否决我在人们感到卡住时不断询问编程问题的地方编程:)问候!是的,这是我错过的事情,high five!为什么这么复杂?为什么要使用filter
进行循环(而不是每个)是的,这是我错过的,high five!为什么这么复杂?为什么要使用过滤器进行循环(而不是每个)?