我可以用Postman罚款提取填充的pdf,但是我的代码仅显示空白的pdf:
async function getWeeklyReport(queryString) {
return new Promise((resolve, reject) => {
request({
url: 'https://reports.myApi.com/ReportCenter/Bookings',
qs: queryString,
headers: {
'Content-Type': 'application/pdf',
'Accept': 'application/pdf',
'Authorization': 'Basic ' + new Buffer(process.env.apiEmail + ':' + process.env.apiPassword, 'utf8').toString('base64')
}
}, (error, response, body) => {
if (error) {
reject(error);
} else {
if (response.statusCode === 200) {
resolve(body);
} else if (response.statusCode === 409) {
setTimeout(() => {
resolve(getWeeklyReport(queryString));
}, response.headers['Retry-After'] * 1000);
} else {
reject(response);
}
}
});
});
}
这是我写的地方:
async function saveCSVLocally(path, obj) {
return new Promise((resolve, reject) => {
fs.writeFile(path, obj, 'utf8', function (err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
console.log(err);
} else{
console.log('File saved locally');
}
});
})
}
我尝试了几种调用方式,但均无效:
const report = await getWeeklyReport(queryString);
//saveFileLocally(localPath, report);//.then(emailSender.sendNewEmail());
//fs.writeFileSync("test.pdf", report,'binary');
那么我如何正确提取此pdf?谢谢!
答案 0 :(得分:0)
确保您正在调用诺言的resolve()
和reject()
函数以完成诺言:
function saveCSVLocally(path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, 'utf8', function (err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
reject(err);
} else {
console.log('File saved locally');
resolve();
}
});
})
}
还:请记住,fs.writeFile()
的第二个参数是一个字符串(不是对象),尽管从技术上讲它可以使用一个碰巧是一个缓冲区。我个人会使用变量data
以避免混淆。