Trying to get a text from a POD

Hi! I’m trying to get some information from a POD but I’m having some problems with the asynchronous functions. I just want a method that receives an url and returns the text of the file that’s on that url. To do that I tried to use some parts of the code of the text editor from the React Generator:

Maybe the problem is that I don’t know how to return that text, but even If I just call another function or try to put that text on a global attribute to call it later, I receive a “Promise” object and I don’t know how to get the text from that object or how to wait until the text is ready (because the function is asynchronous, so I know I have to wait for the text).

Thank you in advance

Try something like this:

const res = await SolidAuth.fetch(url);
if (!res.ok) {
  // handle error...
  throw new Error('error fetching data')
}
const text = await res.text();
return text;
// last two lines could be merged: return res.text();

I think your example should also work if you write return doc.then(...), but I’m not sure. If you are inside an async method I would suggest to use await instead of .then for readability.

2 Likes

@A_A Thank you for answering me!

I talked with some classmates and they recommend me to just use callbacks and change the way that I was imagining the program to work, so now I just wait for the async methods to call that function and then I continue with the rest of the code.

That’s what I did and It’s now working but I’m not satisfied with that class so I’m going to try to change it and maybe use some library.

Anyway, thank you for helping me :grin:

1 Like

Hi @AnaGciaSchz if you want to use some lib, maybe @Vincent cheatsheet could help you Cheatsheet · Tripledoc and there is also @jeffz solid-file-client.

2 Likes

@Smag0 I have just added the solid-file-client library to my project so I can read the file names from a folder, but I was wondering how to use LDflex and that page is perfect, thank you :grin:

Well in fact it depends on what is the extension of your file (.ttl, .json, other…) and if ttl, what you want to do with it…
In general, to get the content as txt you can always use solid-file-client.readFile() function
If you want to get triples in a .ttl file, you can use ldflex-query or tripledoc.
As I understand those libs, ldflex-query make one request per triple opposed to tripledoc that fetch the file in one request, store it and allows you to get props from the store.
If you want to get one triple, use ldflex-query, if you need many properties in the file, tripledoc .
Be careful, if you update data on the Pod, you will perhaps need to clear the cache (ldflex-query & tripledoc) to get the fresh data…

1 Like

a url… lol please an url hurts my ears and thats 2 for 2 in here tonight lol