Posts

Showing posts with the label axios

Axios looping promisses and update previous axios response data

Axios looping promisses and update previous axios response data How I can wait for all promises to be resolve in order to update the response.data of the first ajax call? (example using swapi.co api) A simple express .get wrapper. Each /starship list the pilots resource (see comment inside). I want to have full pilots data in my wrapper /api/starships. app.get('/api/starships/', function(req, res){ axios.get('https://swapi.co/api/starships') .then(function(response){ // res.setHeader('Cache-Control', 'no-control'); // pilots: [ // "https://swapi.co/api/people/13/", // "https://swapi.co/api/people/14/", // "https://swapi.co/api/people/25/", // "https://swapi.co/api/people/31/" // ], response.data.results.forEach(function(starship, i){ if (starship.pilots) { var promises = ; var fullpillots = ; starship.pilots.forEach(function(pilot_inf...

Attach Authorization header for all axios requests

Attach Authorization header for all axios requests I have a react/redux application that fetches a token from an api server. After the user authenticates I'd like to make all axios requests have that token as an Authorization header without having to manually attach it to every request in the action. I'm fairly new to react/redux and am not sure on the best approach and am not finding any quality hits on google. Here is my redux setup: // actions.js import axios from 'axios'; export function loginUser(props) { const url = `https://api.mydomain.com/login/`; const { email, password } = props; const request = axios.post(url, { email, password }); return { type: LOGIN_USER, payload: request }; } export function fetchPages() { /* here is where I'd like the header to be attached automatically if the user has logged in */ const request = axios.get(PAGES_URL); return { type: FETCH_PAGES, payload: request }; } // reducers.js const initi...

Django Rest Framework parsing an array of FormData

Django Rest Framework parsing an array of FormData Here is my Serializer class. I am hoping to have one to many relationship with UserSerializer to SnapCapsulesSerializer. I have allow for SnapCapsule to be added to UserSerializer, so each User will have access to its snapcapsule. Here is the link I am referencing on creating relational Serializers. http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers class UserSerializer(serializers.HyperlinkedModelSerializer): snapcapsules = SnapCapsuleSerializer( many=True, read_only=True, allow_null=True, ) class Meta: model = User fields = ('snapcapsules', 'url', 'username', 'email', 'password') write_only_fields = ('password',) def create(self, validated_data): user = User.objects.create( username=validated_data['username'], email=validated_data['email'], ...

ReactJS - Axios doesn't return data

Image
ReactJS - Axios doesn't return data I have this code to get data with Axios. Unfortunately, this code is not working because the console.log(response); is not logging any data in the console. What is wrong here? Is there something missing? console.log(response); const ROOT_URL = "https://jsonplaceholder.typicode.com"; const getFindAll = (data) => { return { type: constants.FETCH_FIND_ALL, payload: data } } export const fetchFindAll = () => { return (dispatch) => { axios({ method: 'put', url: `${ROOT_URL}/posts/1` }) .then(response => { console.log(response); dispatch(getFindAll(response.data.Body.Message)); }) .catch(error => { //TODO: handle the error when implemented }) } } I'm calling the fetchFindAll() inside FileTree component: fetchFindAll() FileTree export class FileTree extends React.Component { constructor(props) { super(props); this.state = ...