我在运行这个 IBM Cloud Function 时遇到了一些麻烦:
/**
*
* main() will be run when you invoke this action
*
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
*/
function main(params) {
const https = require('https');
https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
我的问题是这个函数的第一次调用(至少前 3-4 次)没有产生输出。后续调用正常运行,日志正确显示。如何解决这种不可预测的行为?当然,我想在第一次调用这个函数时检索我的数据。谢谢。