TextDecoder failing in ES6 Promise recursion - ecmascript-6

I'm attempting to query an API which responds with a ReadableStream of XML.
The code below uses a recursive Promise. Recursive because it sometimes doesn't decode the stream in a singular iteration and this is whats causing my headache.
While I'm successfully fetching the data, for some reason the decoding stage doesn't complete sometimes, which leads me to believe it's when the stream is too large for a single iteration.
componentDidMount() {
fetch("http://thecatapi.com/api/images/get?format=xml&size=med&results_per_page=9")
.then((response) => {
console.log('fetch complete');
this.untangleCats(response);
})
.catch(error => {
this.state.somethingWrong = true;
console.error(error);
});
}
untangleCats({body}) {
let reader = body.getReader(),
string = "",
read;
reader.read().then(read = (result) => {
if(result.done) {
console.log('untangling complete'); // Sometimes not reaching here
this.herdingCats(string);
return;
}
string += new TextDecoder("utf-8").decode(result.value);
}).then(reader.read().then(read));
}

I think that the next iteration was sometimes being called before the current iteration had completed, leading to incorrectly concatenation of the decoded XML.
I converted the function from sync to async and as a regular recursive method of the component rather than a recursive promise with a method.
constructor({mode}) {
super();
this.state = {
mode,
string: "",
cats: [],
somethingWrong: false
};
}
componentDidMount() {
fetch("http://thecatapi.com/api/images/get?format=xml&size=med&results_per_page=9")
.then( response => this.untangleCats( response.body.getReader() ) )
.catch(error => {
this.setState({somethingWrong: true});
console.error(error);
});
}
async untangleCats(reader) {
const {value, done} = await reader.read();
if (done) {
this.herdingCats();
return;
}
this.setState({
string: this.state.string += new TextDecoder("utf-8").decode(value)
});
return this.untangleCats(reader);
}

Related

Javascript - Return json from fetch in an Object

I'm trying to make an application to get the recipes from https://edamam.com and I'm using fetch and Request object.
I need to make 3 request, and i thought that most beautiful way for do it is make an Object and a method that return the data in JSON.
I declarated into constructor a variable called this.dataJson, and i want to save there the data in JSON from the response. For that purpose i use this.
The problem is that i have a undefined variable.
.then( data => {this.dataJson=data;
console.log(data)} )
This is all my code.
class Recipe{
constructor(url){
this.url=url;
this.dataJson;
this.response;
}
getJson(){
var obj;
fetch(new Request(this.url,{method: 'GET'}))
.then( response => response.json())
.then( data => {this.dataJson=data;
console.log(data)} )
.catch( e => console.error( 'Something went wrong' ) );
}
getData(){
console.log("NO UNDFEIND"+this.dataJson);
}
}
const pa= new Recipe('https://api.edamam.com/search?...');
pa.getJson();
pa.getData();
I'm new studying OOP in JS and more new in Fetch requests...
If you guys can help me... Thanks very much!
Here's a solution using async-await (and a placeholder API):
class Recipe {
constructor(url) {
this.url = url;
this.dataJson;
this.response;
}
// the async keyword ensures that this function returns
// a Promise object -> we can use .then() later (1)
async getJson() {
try {
const response = await fetch(new Request(this.url, {
method: 'GET'
}))
const json = await response.json()
this.dataJson = json
} catch (e) {
console.error('Something went wrong', e)
}
}
getData() {
console.log("NO UNDFEIND:", this.dataJson);
}
}
const pa = new Recipe('https://jsonplaceholder.typicode.com/todos/1');
// 1 - here we can use the "then", as pa.getJson() returns
// a Promise object
pa.getJson()
.then(() => {
pa.getData()
});
If we want to stay closer to your code, then:
class Recipe {
constructor(url) {
this.url = url;
this.dataJson;
this.response;
}
getJson() {
// var obj; // not needed
// the "fetch" always returns a Promise object
return fetch(new Request(this.url, { // return the fetch!
method: 'GET'
}))
.then(response => response.json())
.then(data => {
this.dataJson = data;
// console.log(data) // not needed
})
.catch(e => console.error('Something went wrong'));
}
getData() {
console.log("NO UNDFEIND:", this.dataJson); // different syntax here
}
}
const pa = new Recipe('https://jsonplaceholder.typicode.com/todos/1');
// using "then", because the "fetch" returned a Promise object
pa.getJson()
.then(() => {
pa.getData();
});
The problem with your original code is that you initiate the request (pa.getJson()) and then immediately (on the next line) you want to read the data (pa.getData()). pa.getData() is called synchronously (so it happens in milliseconds), but the request is asynchronous - the data needs time to arrive (probably hundreds of milliseconds) - so, it's not there when you try to read it (it simply hasn't arrived yet).
To avoid this you have to use a technique to handle this asynchronous nature of the request:
use a callback function (blee - so last decade)
use a Promise object with then() (much better) or async-await (yeee!)
and call the pa.getData() when the response has arrived (inside the callback function, in the then() or after awaiting the result).

setValue in formArray angular 8 and show ui

I want show initial data in my formArray
i can set value and show value in console log but dont show this data in the ui form
ngOnInit() {
this.getCertificate(this.id);
this.assessmentForm = this.fb.group({
certificateArray: this.fb.array([ this.createItem() ]),
});
}
createItem(): FormGroup {
return this.fb.group({
confirm: '',
score: '',
description: ''
});
}
getCertificate(id) {
this.certificateList = [];
this.UsersRegisterService.getCertificate(id).subscribe((res: any[]) => {
this.certificateList = res;
var index=0;
this.certificateList.forEach(element => {
this.AssessmentService.getCertificateAssessment(element.id.value).subscribe((res: any[]) => {
if(res!=null){
this.certificateArray.at(index).setValue(
{ confirm: res['confirm'], score: res['score']['value'],description:res['description']});
console.log( this.assessmentForm['controls'].certificateArray['controls'][index]['controls'].score.value);
}
});
index++;
});
});
}
i set value this method
this.certificateArray.at(index).setValue(
{ confirm: res['confirm'], score: res['score']})
please help me how can i show this value in the Ui form
Use patchValue
this.certificateArray.at(index).patchValue(res);
Note that you would never subscribe inside a subscription, and even less often (never never) subscribe in a forEach. pipe the data instead.
You can refactor your code a little bit, right now you have a shadowed name res. I would log responses from each call to getCertificateAssessment to make sure you're getting what you expect:
getCertificate(id) {
this.certificateList = []; // this should be set at the top of your component
this.UsersRegisterService.getCertificate(id).pipe(
catchError(err => {
console.log('get cert error', err);
return [];
})
).subscribe((list) => {
this.certificateList = list;
this.certificateList.forEach((element, i) => {
this.AssessmentService.getCertificateAssessment(element.id.value).pipe(
catchError(err => {
console.log('get assessment error', err);
return null;
})
).subscribe((res) => {
if (res) {
console.log('res', i, res); // be sure of response
this.certificateArray.at(i).setValue({
confirm: res.confirm,
score: res.score.value,
description: res.description
});
} else {
console.log('no res!');
}
});
});
});
}
Chris makes a good point about piping but I'm guessing these service calls are http requests so they complete just like a promise.
I also added catchError to catch errors with the service calls.
Plus you are having to make a call for each assessment which makes for a lot of calls. Maybe refactor your backend to make it 1 call?
If you refactored the endpoint for getCertficateAssessment to accept an array of values and return an array of responses, you could do this:
this.UsersRegisterService.getCertificate(id).pipe(
switchMap(list => this.AssessmentService.getCertificateAssessment(list.map(l => l.id.value)))
);
You would need to create an id for each assessment so you could assign them.

Angular 4 wait till http.get execute to continue

I'm creating a new register form an app with Ionic and using ASP.Net(C#) as my API.
I want to check if user exists when the input blur event is activate.
The problem is that my code isn't waiting till the server returns a value to continue.
What am I doing wrong? Is there a way to do that?
THIS IS MY API CODE:
[HttpGet]
public JsonResult verifyEmail(string email)
{
var result = Domain.Repository.UserController.Find(email:email);
if (result != null)
{
return Json(new { erro = true, message = "Email already registered!" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { erro=false,message = "Email is valid!" },JsonRequestBehavior.AllowGet);
}
}
I CREATED A PROVIDER TO MAKE THE HTTP REQUEST(authProvider):
getData(data,func)
{
return new Promise( (resolve,reject)=>{
this.http.get(apiUrl+func, {params:data})
.subscribe(
res=>{
resolve(res.json());
},
async (err)=>{
reject(err);
});
});
}
AND HERE IS MY register.ts code:
validate()
{
let validEmail;
validEmail= this.checkEmail();// I WANT THAT the "validEmail" receives returned value before continue.
return true;
}
AND THE LAST THING IS MY FUNCTION THAT WILL CALL THE PROVIDER:
checkEmail()
{
return this.authService.getData({email:this.model.email},"Account/verifyEmail").then((result)=>{
let response = <any>{};
response=result;
if(response.erro)
{
return response.message
}else
{
return true
}
},(err)=>{
this.toastService.presentToast("ERROR:"+err,"bottom",undefined,"toast-error");
});
}
Thanks in advance..
getData(data,func)
{
this.http.get(apiUrl+func, {params:data})
.map(res => {
return res.json();
})
.toPromise();
}
or with async/await
async getData(data,func)
{
let result = await this.http.get(apiUrl+func, {params:data})
.toPromise();
return result.json();
}
Now for the validate function:
async validate()
{
let validEmail;
await this.checkEmail();
return true;
}
Point is you cant jump from a sync function to an async or vice versa.
Validate needs to return a promise/observable because it is executes asynchronous functions.

How to execute promise of super class in ES6

I've a promise in Parent class, whenever I call the promise from child class, it is returning the undefined, instead of executing the promise and returning the resul.
import {newsApiKey as APIKEY, newUrl as APIURL} from "./secretToken";
class News{
constructor(){
this.token = APIKEY;
this.url = APIURL;
this.source = 'bbc-news&';
}
topNews(){
const bbcNews = fetch(`${this.url}?source=${this.source}&sortBy=top&apiKey=${this.token}`);
bbcNews.then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json()
})
.then(json => {
console.log(json.articles);
return json.articles;
})
.catch((err) => {
return err.message;
});
}
}
export { News as default};
CHILD CLASS
import News from "./news";
class StickyNote extends News{
displayNews(){
let bbcNews = super.topNews(); // It is returning only undefined
if (typeof bbcNews != 'undefined') {
console.log(bbcNews); //
}
}
}
topNews never returns anything, so the result of calling it is undefined.
You probably wanted a return here:
topNews() {
const bbcNews = fetch(`${this.url}?source=${this.source}&sortBy=top&apiKey=${this.token}`);
return bbcNews.then(response => {
// ^^^^^^
if (!response.ok) {
throw Error(response.statusText);
}
return response.json()
})
.then(json => {
console.log(json.articles);
return json.articles;
})
.catch((err) => {
return err.message;
});
}
Also note that displayNews will need to use the promise it receives:
displayNews(){
super.topNews().then(articles => {
// ...use articles
});
}
(Normally you'd also have a catch there at the endpoint of consumption, but as you've converted rejections into resolutions...)
Note: That code has a bit of an anti-pattern in it: It converts a rejection into a resolution with an error message. Anything using the promise will never see a rejection, only resolutions with varying return types (whatever json.articles is or a string). In general, it's better to allow rejections to propagate, and handle them at the ultimate point of consumption of the entire chain (displayNews, I believe, in your example). You might transform their content, but not convert them from a rejection into a resolution.
FWIW, I'd probably rewrite that like so:
topNews() {
return fetch(`${this.url}?source=${this.source}&sortBy=top&apiKey=${this.token}`)
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then(data => { // "data", not "json" -- it's not JSON anymore
return data.articles;
});
}
...which ensures that the caller either gets a resolution with the articles, or a rejection with an Error, so:
displayNews(){
super.topNews()
.then(articles => {
// ...use articles
})
.catch(err => {
// ...show error
});
}

React Native : render a fetched data structure

I am new to Javascript (NodeJS & JSON data structure) and React native, and I am trying to display some datas on the iOS screen test (react native). I did my best to understand but I am stuck.
I made a GET route on my Express app at /places on localhost:3000 :
router.get('/places', (req, res) => {
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
var colPlaces = db.collection('places');
colPlaces.find().toArray(function (err,result) {
if (err) {
res.send('error!');
} else if (result.length) {
res.send(result);
} else {
res.send('No entries');
}
})
db.close();
}});
})
It displays a data structure on my browser (seems to be an Array of JSON objects ?)
[
{"_id":"5894fdd694f5d015dc0962bc","name":"The good bar","city":"London"},
{"_id":"5894fdd694f5d015dc0962bd","name":"Alpha bar","city":"Paris"}
]
I am then trying to render it on my native react project :
constructor(props) {
super(props);
var datas = async function getDatas() {
try {
let response = await fetch('http://localhost:3000/places');
let responseJson = await response.json();
return responseJson;
} catch(error) {
console.error(error); }
}
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(datas())
};
}
render() {
return (
<View style={{flex: 1, paddingTop: 22}}>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
</View>
);
}
But all I got is two "0" on my screen.
Can someone help me to find out what is wrong ?
What is/are,
* "result" variable structure ? seems to be an array of JS objects.
* "response" and "responseJSON" variables ?
* "datas()" and dataSource type
Should I parse something ? I am a bit confuse. Thank you.
First, "data" is already plural, there is no "datas". :) Now back to your question, getData is async function, so you cannot just call getData() and expect it to return data immediately. In the constructor, set dataSource to empty list, then:
async function getData() {
try {
let response = await fetch('http://localhost:3000/places');
return response.json(); // this call is not async
} catch(error) {
console.error(error);
}
}
componentDidMount() {
let data = await this.getData();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({dataSource: ds.cloneWithRows(data)});
}