admin管理员组文章数量:1416305
I'm facing what seems to be a mon problem, however, I really cannot figure out how to solve it.
I'm calling a function with makes a GET request, parses the returned JSON, and then (supposedly) returns a Promise, which I use to print the parsed JSON data. However, I am getting the error described in the title of this question when I try to do so. I have two files that I'm using. The first simply calls a function from the second, and tries to print the return value (and gets the error).
First file:
var secondFile = require('./test');
secondFile.testFunc("some stuff").then(function(res) {
console.log(res);
})
Second file (test.js):
module.exports = {
testFunc : function(address) {
var params = {
q: address,
format: "json"
}
var baseUrl = "";
rp({url: baseUrl,
qs: params,
method: "GET"})
.then(function(body) {
var parsedBody = JSON.parse(body);
var result = {one: parsedBody[0], two: parseBody[1]};
return new Promise(function(resolve, reject) {
resolve(result);
});
}).catch(function(err) {
console.log(err);
});
}
}
Any help is greatly appreciated.
I'm facing what seems to be a mon problem, however, I really cannot figure out how to solve it.
I'm calling a function with makes a GET request, parses the returned JSON, and then (supposedly) returns a Promise, which I use to print the parsed JSON data. However, I am getting the error described in the title of this question when I try to do so. I have two files that I'm using. The first simply calls a function from the second, and tries to print the return value (and gets the error).
First file:
var secondFile = require('./test');
secondFile.testFunc("some stuff").then(function(res) {
console.log(res);
})
Second file (test.js):
module.exports = {
testFunc : function(address) {
var params = {
q: address,
format: "json"
}
var baseUrl = "http://google.";
rp({url: baseUrl,
qs: params,
method: "GET"})
.then(function(body) {
var parsedBody = JSON.parse(body);
var result = {one: parsedBody[0], two: parseBody[1]};
return new Promise(function(resolve, reject) {
resolve(result);
});
}).catch(function(err) {
console.log(err);
});
}
}
Any help is greatly appreciated.
Share Improve this question asked Feb 26, 2017 at 0:35 Miki PMiki P 6521 gold badge9 silver badges22 bronze badges 7-
2
FYI,
return new Promise(function(resolve, reject) {resolve(result);});
can be replaced withreturn result;
. Inside a.then()
handler, you can just return a value and that will bee the resolved value of the parent promise. – jfriend00 Commented Feb 26, 2017 at 0:39 - Thanks! That does make it a bit nicer to read. – Miki P Commented Feb 26, 2017 at 0:54
-
In cases where you do need to return a resolved promise, you can also do
return Promise.resolve(result);
, though that is not needed here. – jfriend00 Commented Feb 26, 2017 at 1:45 - @jfriend00 I'm wondering if it would somehow be possible to call testFunct, then call it again, and then use the results of both functions after they've resolved. – Miki P Commented Feb 26, 2017 at 2:40
-
Yea, get two promises, then use
Promise.all([p1, p2]).then(results => { console.log(results)});
Promise.all()
tracks all the promises you pass it and call's it's.then()
handler when they have all finished and gives you an array of results in the order you passed the promises. – jfriend00 Commented Feb 26, 2017 at 3:34
1 Answer
Reset to default 6testFunc
doesn't have a return
statement, so it returns undefined
.
If you want to take the return value of rp
(which appears to be a promise) and then return that from testFunc
then you need to add a return
statement.
return rp({url: baseUrl,
本文标签:
版权声明:本文标题:javascript - "cannot read property 'then' of undefined", despite returning a Promise - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745253431a2649957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论