I have code like this
GetRequests(requestStartId,requestEndId);
async function GetRequests(startId,endId){
let fileAddress = __dirname + '\\attachments';
for (let i = startId; i <= endId; i++) {
if (fs.existsSync(fileAddress + '\\' + i)) {
await upload(i,fileAddress);
}
}
}
/**
upload files
**/
async function upload(i,fileAddress){
fs.readdir(fileAddress + '\\' + i, (err, files) => {
const newID=await getNewRequestID(i);
files.forEach(file => {
...
...
...
await axios({
method: 'post',
url: `my_url`,
data: form,
headers: {
[my_header_items]
}
}).then(function (response) {
// handle success
console.log(response);
}).catch(function (error) {
console.log(error);
});
});
});
}
async function getNewRequestID(id) {
let inputData = {
[my_input_data]
};
form.append('input_data', JSON.stringify(inputData));
return await axios({
method: 'get',
url: `my_url,
data: form,
headers: {
[my_header_item]
}
}).then(function (response) {
return response.data.id;
}).catch(function (error) {
console.log(error);
});
}
when I run this script show me this error
const newID=await getNewRequestID(i); ^^^^^ SyntaxError: await is only valid in async function
Put async
before the callback function in the following line...
fs.readdir(fileAddress + '\\' + i, async (err, files) => { ...
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.