How can I use conditionally make an API call using AXIOS? [closed] - reactjs

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using AXIOS with REACTjs
I want a functionality that if my first API(GET) call returns Not Found
status code then I want to send the other call efficiently.
Although I'm getting some data through that .catch method in form of Promise<pending> .Please Help!!!

Why don’t you add a conditional in the catch block to check the status?
For example:
axios.get('/first-call')
.then(resp => {
//handle successful response
})
.catch(error => {
if (error.response && error.response.status === 404) {
// make second API call
}
});

Related

Make a api call every 10mins in an hour from react component [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I want to make one api call every 10mins in an hour (like 10am, 10:10am, 10:20am, 10:30am ...) from my react component. If I use setInterval, timer starts based on when component is loading. But I have a use case where I need to make constant 10mins in an hours(it should be 10am, 10:10am, 10:20am, 10:30am. not like 10:04am, 10:14am) Could someone help me to do this in a better way without affecting app performance?
Here is how I would do with a basic example :
function makeApiCall() {
// API logic here
}
const scheduleApiCall = () => {
const now = new Date();
const nextInterval = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes() + (10 - now.getMinutes() % 10),
0,
0
);
const timeToNextInterval = nextInterval - now;
setTimeout(() => {
makeApiCall();
scheduleApiCall();
}, timeToNextInterval);
}
scheduleApiCall();

Laravel, get data from api ans save it in database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i installed guzzle the laravel package, and i display the data from an api. I want to save this data in database. I can't find enough resources to do it
route
controller
response
public function all() {
$response = $client->request('GET', 'http://api'); // Pass the third parameter as an array if you have to set headers
$response = json_decode($response->getBody(), true); // The API response data
// You should put your data in the loop.
$data = collect($response); // So we create an collection to use helper methods
$data->map(function ($item) {
// Now, we can save the data to the database with two methods
// Model:
YourModel::create([
'column' => $item['key'],
]);
// Query builder:
DB::table('your_table')->insert([
'column' => $item['key'],
]);
});
}

Overriding the redirect on saving force:createRecord on Salesforce Lightning [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to override the redirect on save of a force:createRecord lightning component.
var eve = $A.get("e.force:createRecord");
eve.setParams({
"entityApiName": "Opportunity",
"defaultFieldValues": {
"AccountId" : accountId
},
"panelOnDestroyCallback": function(event) {
console.log('test');
//console.log(event.getParam("id"));
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
"url": "<some visualforce page url>",
"isredirect": "true"
});
urlEvent.fire();
}
});
eve.fire();

Store GIF from URL as object variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
https://media2.giphy.com/media/JpYdtQifMv3SAsnf8j/giphy.gif?cid=f25c51ea3ef14717fe448ab031d8a047c77d1438043fcca6&rid=giphy.gif
I want to extract the image from the URL above as an object variable, or BLOB, or whatever would be best.
How do I do this ?
I tried Get with axios and it did not work.
The end goal is to upload the object to an S3 folder later.
You can just use fetch here. Some examples list how to load images via the blob body api
async function loadData(url) {
const resp = await fetch(url)
const data = await resp.blob()
const imgUrl = URL.createObjectURL(data);
return imgUrl
}
where you can set an image.src to be this url

How to replace HTTP with a simple string in angular.js? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am started to work with Angular.js, I want to replace HTTP:// of any URL in a simple string like 'hey', I didn't get any relevant solution.
I'd be grateful for your response.
try this.
http://www.w3schools.com/jsref/jsref_replace.asp
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
I got my answer here is the controller code
function ListCtrl($scope, $http,Project) {
$http.get('/project').success(function(data){
for(var i=0;i<data.length;i++){
data[i].site=data[i].site.replace('http://','Hey');
}
$scope.projects=data;
});

Resources