I try to receive data from gitHub API. As the name is entered in input element I send a request to
https://api.github.com/search/users?q=${username} in:login type:user.
And I`m expecting that I received all users with this username (input element may has whole username or just a part of username). But got only one user.
You can use async and await method to get your proper result.
const gitUsers = async (username) => {
const response = await fetch(`https://api.github.com/search/users?q=${username}`);
const result = await response.json();
console.log(result);
};
gitUsers("carl")
Related
I am coming across a 429 error while fetching a lot of urls at the same time, in useEffect. I am trying to fetch prices of stocks by mapping the names from the array shortNames using a proxy server called allorigins
The function that fetches the data:
const fetchData = async (url) => {
try {
const response = await fetch(url)
if (!response.ok) throw response.statusText
const data = response.json()
return data
} catch (error) {
console.error(error)
}
};
The price_response stores the price data in an array after the Promises are resolved:
const price_response = await Promise.all(shortNames.map((stock) =>
fetchData(`https://api.allorigins.win/raw?url=https://query1.finance.yahoo.com/v8/finance/chart/${stock}.BO`)
));
This async method of fetching throws error when it loads the first time. Refreshing the page adds more data to price_response retrieving some already fetched data from the disk-cache.
I am trying to find a better way, as to not make the user refresh the page again and again.
Is there a way I can fetch 60-70 urls using async or even sync function? Also, can I use disk-cache in a more customised way to make this work on the very first load?
I am trying to make an information board that when user press for more information, it will do a get /:id to the backend where it will grab the data and display it in the popup
You can do something like:
const onClickHandler= async () => {
const res = await fetch(endpoint);
const data = await res.json();
setData(data)
}
But using try catch and the rest of things to validate a successful req.
I want to sign a transaction of a user from phantom wallet and then send the transaction through web3.js but after successfully signing the transaction the web3js library function sendRawTransaction() is giving error message in console
const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);
If you look at the implementation of sendTransaction, you'll see that it's adding a blockhash to the transaction before signing, serializing, and sending. Without a blockhash, you'll get that error Blockhash not found. So instead, you need to do something like:
const latestBlockhash = await connection.getLatestBlockhash();
transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
transaction.recentBlockhash = latestBlockhash.blockhash;
const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);
Full implementation of sendTransaction at https://github.com/solana-labs/solana/blob/3fcdc45092b969baeb7273de6596399d98277366/web3.js/src/connection.ts#L4389
So I'm using Movies API from TMDB API, and my code looks like this:
let apikey = '{my_apikey}';
let url: string = 'https://api.themoviedb.org/3/movie/apikey=';
url = url + apikey;
console.log(url);
(async function () {
const response = await fetch(url);
const data = await response.json();
console.log(data);
})()
If I insert the url along with my API it displays the data just fine, but when I'm doing it from my app, I have the next error:
I can't figure out what could be the problem, cause my api key is fine and I copied it from the website.
According to the API documentation. The URL should look something like this:
https://api.themoviedb.org/3/movie/76341?api_key=<<api_key>>
I have written an API where I am generating data in JSON format.
Now I am calling same API on react frontend. If response is generated we will display a success page else we will divert to same page. So I have written these code when I submit user id and password on submit event validateLogin API has been called, but I am not able to execute body of API response.
onSubmit = async => {
const { user_mail, user_pass1 } = this.state
const payload = { user_mail, user_pass1}
api.validateLogin(payload).then((res) =>{
alert('Success');
})
.catch((err) =>{
alert('Failed');
})
}
Here nothing is executing. Can you please help me to execute response body?