Javascript Firebase:函数返回了未定义的预期承诺或值
我正在编写一个Firebase函数,其中我遵循中示例代码中的代码 然而,我一直在犯错误Javascript Firebase:函数返回了未定义的预期承诺或值,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,我正在编写一个Firebase函数,其中我遵循中示例代码中的代码 然而,我一直在犯错误 函数在我的Firebase函数日志中返回了未定义的预期承诺或值 我几乎修改了我的代码,使之完全相同,但没有喘息的机会。有人试过这个代码吗?它是无错误的吗?为什么我会出错?同样的代码也在 下面是产生错误的示例代码 exports.imageToJPG = functions.storage.object().onChange(event => { const object = event.data
函数在我的Firebase函数日志中返回了未定义的预期承诺或值
我几乎修改了我的代码,使之完全相同,但没有喘息的机会。有人试过这个代码吗?它是无错误的吗?为什么我会出错?同样的代码也在
下面是产生错误的示例代码
exports.imageToJPG = functions.storage.object().onChange(event => {
const object = event.data;
const filePath = object.name;
const baseFileName = path.basename(filePath, path.extname(filePath));
const fileDir = path.dirname(filePath);
const JPEGFilePath = path.normalize(path.format({dir: fileDir, name: baseFileName, ext: JPEG_EXTENSION}));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalJPEGFile = path.join(os.tmpdir(), JPEGFilePath);
// Exit if this is triggered on a file that is not an image.
if (!object.contentType.startsWith('image/')) {
console.log('This is not an image.');
return;
}
// Exit if the image is already a JPEG.
if (object.contentType.startsWith('image/jpeg')) {
console.log('Already a JPEG.');
return;
}
// Exit if this is a move or deletion event.
if (object.resourceState === 'not_exists') {
console.log('This is a deletion event.');
return;
}
const bucket = gcs.bucket(object.bucket);
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
return bucket.file(filePath).download({destination: tempLocalFile});
}).then(() => {
console.log('The file has been downloaded to',
tempLocalFile);
// Convert the image to JPEG using ImageMagick.
return spawn('convert', [tempLocalFile, tempLocalJPEGFile]);
}).then(() => {
console.log('JPEG image created at', tempLocalJPEGFile);
// Uploading the JPEG image.
return bucket.upload(tempLocalJPEGFile, {destination: JPEGFilePath});
}).then(() => {
console.log('JPEG image uploaded to Storage at', JPEGFilePath);
// Once the image has been converted delete the local files to free up disk space.
fs.unlinkSync(tempLocalJPEGFile);
fs.unlinkSync(tempLocalFile);
});
});
有什么建议吗?最近Firebase似乎更新了他们的SDK,因为他们的示例代码和文档很少过时。即使您正试图退出函数,也必须返回带有布尔值的。因此,对于上面代码中未返回承诺的每个返回语句
,必须是return true
一旦Firebase更新了示例代码和文档,我将删除此问题和答案。在此之前,请将其留给那些可能仍在不知道原因的情况下偶然发现此问题的人。“寻求调试帮助的问题('此代码为什么不起作用?'))必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建问题。示例代码和文档尚未更新,以反映SDK中最近的更改。看到这个问题:,特别是弗兰克的评论。@BobSnyder-谢谢Bob,你的评论非常有用。我设法找到了这个问题的答案。而你却很兴奋。感激这一点不确定你必须在哪里返回true!?在您的例子中,您有returnbucket.upload(templacaljpegfile,{destination:JPEGFilePath})代码>如何返回true@FamicTech-在前3条返回语句中,如果在非图像文件上触发,则注释//后的语句将退出。//如果图像已经是JPEG,则退出;如果是移动或删除事件,则退出