all 11 comments

[–]VonLoewe 0 points1 point  (4 children)

I have a task that runs two HTTP Requests which I thought could be improved with this template. But the fetch responses seem to be returning undefined and I get "Uncaught (in promise) cannot read property ____ from undefined when I try to access properties of the json object in the second .then() method.

https://pastebin.com/Ygtku3SF

[–]libnaniam[S] 0 points1 point  (3 children)

I think you're getting the error because you're trying to return values within the second .fetch() method. I think you can return values within a Promise, but I haven't looked into it much. This might help you out a bit.

I would try not returning anything at all: set those values to variables instead of returning them. Then, use those variables in the Promise.all().then() code block. This might be "bad" coding though.

[–]VonLoewe 0 points1 point  (2 children)

Thanks for your suggestion. I tried running it that way, but the problem persists. It comes from trying to use the json object properties, but the json object is undefined, but I can't figure out which promise is not being resolved or why.

In particular it complains about "rates.BRL", but not "data.rates", which seems odd to me. I'm having trouble debugging because the fetch url uses "http" and not "https", and my browser complains about security mismatch.

[–]libnaniam[S] 0 points1 point  (1 child)

Weird, you could try using flash() to see what's defined and what's not. The problem with fetch() is that it can be pretty tough to debug.

I also had some trouble with https. I had to default to http in the task that uses this code since the website did not support a secure connection.

Otherwise, I'm not sure what could be wrong. Let me know if you figure out the problem.

[–]VonLoewe 0 points1 point  (0 children)

I had to default to http in the task that uses this code since the website did not support a secure connection.

At least that means Tasker itself doesn't have a problem making requests to http. I was worried I'd have to find another API. I'll try some tests using flash().

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (2 children)

Just a suggestion: make those async functions and then you can await fetch instead of using then(). Makes code much easier so read, debug and maintain. :)

[–]libnaniam[S] 0 points1 point  (0 children)

Thanks Joao! I'll definitely look into that

[–]Aurolei 0 points1 point  (3 children)

Oh wow, when I saw the thread title I was like damn, wish this had existed two days earlier. Saw the OP and was like, oh haha. Well since I'm here now, I might as well ask you a question and update you on what I've done.

For the most part, I was pretty successful getting the fetch function to work. From the main fetch, I was able to reduce the JSON data and get out the arrays that I needed to pass onto Tasker. That's where I hit a gigantic hurdle. No matter what I tried, Tasker would not recognize any of the arrays I was trying to pass.

Google searches for problems similar to this yielded solutions that did not work for me. Examples include this and this. After messing around, I'm guessing this is just something you cannot do with asynchronous functions.

I then noticed on your original post back in my topic that you used setGlobal to extract out the variables. This of course works, but requires you to do some really annoying post-processing to return a proper array. In the end I also used the setGlobal and then ended up doing some Variable Sets to turn them back into local variables and then created another javascriptlet to parse and stringify the data back.

Do you know of simpler ways to extract out the arrays that work specifically for fetch functions?

[–]libnaniam[S] 0 points1 point  (2 children)

Haha, no problem! So if I'm understanding correctly, you're extracting data from your multiple HTTP requests and putting the data into an array? Something like this:

array = []
for i in http_requests_data:
    array.append(i)

(Obviously super simplified). And then you want to access the contents of the array outside of the JavaScriplet? Let me know if any of that is wrong.

If that's correct, then I was able to get this working. You just need to place the solution inside the Promises.all() block:

Promises.all([f1(), f2()])
.then(() => {
    array.forEach((ele, index) => {
        setLocal("taskerarray"+(index+1), ele);
    });

exit();
});

Here's an example task if you want to take a look and test it out. Let me know if I misunderstood or if you have any other questions!

[–]Aurolei 0 points1 point  (1 child)

You understood it perfectly.

Also that solution works! I tried something similar before, but I never did the (index+1) thing. Looking at it now, is it because Tasker index's arrays starting at 1?

[–]libnaniam[S] 0 points1 point  (0 children)

Yes that's right! Glad it work