Abh*_*hek 2 javascript firebase google-cloud-functions
我正在编写Firebase函数,我在跟踪Firebase提供的Github中的示例代码中的代码
但是,我一直Function returned undefined, expected Promise or value在Firebase函数日志中收到错误
.
我几乎修改了我的代码,但却没有任何喘息的机会.有人试过这段代码吗?它没有错误吗?为什么我收到错误?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);
});
});
Run Code Online (Sandbox Code Playgroud)
有什么指针吗?
Abh*_*hek 10
最近Firebase似乎更新了他们的SDK,因为他们的示例代码和文档很少过时.return即使您只是尝试退出该函数,也必须使用布尔值.因此,它必须return true为每个return statements在上述地方没有返回无极代码.
一旦Firebase更新了示例代码和文档,我将删除此问题并回答.然后将其留在这里,以便那些可能在不知道原因的情况下偶然发现这个问题的人.
| 归档时间: |
|
| 查看次数: |
3821 次 |
| 最近记录: |