I have an object that looks like this and stores some attributes:
Basically what I need is to loop through this object and get the file attribute of every key that I would later use in my code to generate multiple Video-React components. I tried to use map() function but the site will crash with the error:
Parsing error: Can not use keyword 'await' outside an async function.
Even though the code sample that I am working with is inside an async function.
Here is my code:
const response = await dataProvider(GET_MANY, 'vid', { ids: videoId })
const file = response.data;
file.map( source => {
var videoPosition = 0;
var sigkey = "sigkey";
var formBody = new FormData();
formBody.set('ver', "1.2");
formBody.set('key', "key");
formBody.set('video_id', file[videoPosition].file);
formBody.set('user_id', "1234");
formBody.set('format', "json");
formBody.set('ip', "");
formBody.set('tts', "0");
formBody.set('nonce', Math.round((new Date()).getTime() / 1000));
var sign_fields = [formBody.get('video_id'), formBody.get('user_id'), formBody.get('ip'), formBody.get('tts'), formBody.get('ver'), formBody.get('key'), formBody.get('nonce')];
var data = sign_fields.join(':');
var signature = hmacsha256(data, sigkey);
formBody.set('sig', signature);
var formBodyStringified = new URLSearchParams(formBody).toString();
const resJson = await fetch(Config.api.livebox, {
method: 'POST',
body: formBodyStringified,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then(res => res.json());
const finalJsonUrl = 'https:' + resJson.hls;
this.setState({ videoSource: finalJsonUrl });
videoPosition++;
});
Any suggestions how can I loop through the object?
Thank you in advance.

for ofinstead of map. Also your not using the return of map, so forEach would have been more logical.async. and inside thatasyncfunction you can use as muchawaitawait fetchdoes not occur in anasyncfunction.asynckeyword in front of the callback passed toresponse.map, May I also suggest you put the map inside of a Promise.all