我正在编写Firebase函数,我正在关注Github provided by Firebase中示例代码中的代码
但是,我一直在收到错误
我的Firebase功能日志中的Function returned undefined, expected Promise or value
。
我几乎修改了我的代码,但却没有喘息的机会。有人试过这段代码吗?它没有错误吗?为什么我收到错误?同样的代码也在Firebase guide
中产生错误的示例代码在
之下 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);
});
});
任何指针?
答案 0 :(得分:8)
最近Firebase似乎更新了他们的SDK,因为他们的示例代码和文档很少过时。即使您只是尝试退出该函数,您也必须return
使用布尔值。因此,对于上面代码中没有返回Promise的每个return true
,它必须是return statements
。
一旦Firebase更新了示例代码和文档,我将删除此问题并回答。然后将其留在这里,以便那些可能在不知道原因的情况下偶然发现这个问题的人。