I got in a problem that I have to combine thow objects and return in one function. The problem is that for both objects I have almost the same properties, but the values are different.
So I created two separated functions that have each one same object, almost same props, diff values. Inside each function I have a url that gets called for both cases different: case1 = skoda, case2 = toyota.
The problem is that I don't know how to make thow diff get calls(I put a comment there) to have both cars returned in one function, to be listed in one xml link. Now each car is on a separate xml link, because there are in two separated files. Now has to be one link and I can't figure out how to do this.
Please someone can give me a little help?
const env = process.env.NODE_ENV || 'development';
const apiUrl = config.api[env];
const skodaParams = {
categoryId: 'skoda',
limit: 1000,
offset: 0
};
const toyotaParams = {
categoryId: 'toyota',
limit: 1000,
offset: 0
};
const generateSkoda = skoda => {
// a lot of code for skoda prop
const skodaList = {
'g:id': product.carId
'g:brand': product.brand,
'g:color': product.color
'g:teamSkoda: 'Skoda'
}
return skodaList;
}
const generateToyota = toyota => {
// a lot of code for toyota prop
const skodaList = {
'g:id': product.carId
'g:brand': product.brand,
'g:color': product.color,
'g:teamToyota: 'Toyota'
}
return skodaList;
}
const getSkoda = () => {
return axios.get(apiUrl, { params: skodaParams });
}
const getToyota = () => {
return axios.get(apiUrl, { params: toyotaParams });
}
const createBothCars = () => {
// here I wanted to add some how two axios get calls for both skoda and toyota
return axios
.all([getSkoda(), getToyota()])
.then(response => {
console.log('response', response);
const skodaArray = [];
response.data.skoda.map(item => {
return skodaArray.push(generateSkoda(item));
})
const Obj = {
feed: {
$: {
xmlns: 'http://www.w3.org/2005/Atom',
'xmlns:g': 'http://base.google.com/ns/1.0'
},
title: 'Skoda cars list',
link: {
$: {
rel: 'self',
href: config.domain
}
},
entry: skodaArray
}
};
const objBuilder = new xml2js.Builder({
cdata: true
});
return objBuilder.buildObject(Obj);
})
.catch(error => {
console.log(error);
});
};
module.exports = createXml;
And in server.js I have:
const createXml = require('../src/xml/createXml');
app
.prepare()
.then(() => {
const server = express();
server.get('/both-cars.xml', (req, res) => {
res.header('Content-Type', 'application/xml');
(async function sendXML() {
const xmlFile = await createXml();
res.send(xmlFile);
})();
});
Update
I updated the code with axios.all, following the comment, but in the response I don't get values from both functions, but I get the values from the last function listed in this array:
[getSkoda(), getToyota()]
now I get the values only from getToyota,
data: {
filters: [Array],
skoda: [],
toyota: [Array],
_version: 'Fri Jun 19 14:43:47 EEST 2020',
errorCode: 'no_error'
}
if I switch the order [getToyota(), getSkoda()] I will get only the values from getSkoda.
This is from console.log:
data: {
filters: [Array],
skoda: [Array],
toyota: null,
_version: 'Fri Jun 19 14:47:09 EEST 2020',
errorCode: 'no_error'
}
Any other help?
In response you should receive two values:
return axios
.all([getSkoda(), getToyota()])
.then((responseOne, responseTwo) => {
console.log('responseOne', responseOne);
console.log('responseOne', responseOne);
Here is the example code that may help further:
axios.all([getToyota(), getSkoda()])
.then(axios.spread(function (toyotaResponse, skodaResponse) {
// Both requests are now complete
console.log('toyota Response', toyotaResponse);
console.log('skoda Response', skodaResponse);
}))
;
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.