here is my full function, it is from a React-Native project using redux and making a ajax call with axios and redux-thunk:
export function FetchEtiquetas() {
return function (dispatch) {
axios.get( url )
.then(response => {
response.data.map( record =>
{
axios.get('https://e.dgyd.com.ar/wp-json/wp/v2/media?_embed&parent='+record.id)
.then(response => {
// compose the new json string with each post gallery
let data_json = '[';
response.data.map( record =>
data_json = data_json + '{ "title": "' + record.title.rendered + '", "subtitle": "'+ record.caption.rendered.slice(3, -5) + '", "illustration": "' + record.source_url + '"},'
);
data_json = data_json.slice(0, -1)+"]"; // removes last comma
record.gallery = date_json;
console.log("date_json record.gallery: "+record.gallery);
})
.catch(error => {
console.log(error.response)
});
console.log("gallery in data action: "+record.gallery);
}
);
dispatch({ type: FETCH_ETIQUETAS_SUCCESS, payload: response.data })
}
);
}
}
In the map function, this is the record format:
{
id: 521,
date: "2014-04-16T16:24:04",
date_gmt: "2014-04-16T16:24:04",
modified: "2018-01-12T23:58:22"
}
and this is the data_json format once it completed:
[
{ "title": "img6", "subtitle": "", "illustration": "domain.com/img6.jpg"},
{ "title": "img7", "subtitle": "", "illustration": "domain.com/img7.jpg"},
{ "title": "img8", "subtitle": "", "illustration": "domain.com/img8.jpg"}
]
How can I add data_json to each record?
thanks!
I could do it by updating the asignment to:
record.gallery = JSON.parse(data_json);
It was a simple change but I figured it out thanks to what was pointed out in the comments:
The original object is not a JSON object, but a plain javascript object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.