2017-12-24 18 views
1

は私がGithub provided by Firebase のサンプルコードからコードを次のですが、私は一貫して私のFirebase機能で 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); 
    }); 
}); 

任意のポインタの下にあるFirebase guide

でもありますか?

+0

"デバッグのヘルプを求める質問(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、および問題そのものを再現するために必要な最短コードが含まれている必要があります。他の読者にとって役に立ちません。参照してください:[最小限の、完全で検証可能な例(http://stackoverflow.com/help/mcve)]を作成する方法。 –

+0

サンプルコードとドキュメントは、SDKの最近の変更を反映するように更新されていません。この質問を参照してください:https://stackoverflow.com/q/47128440/4815718、特にフランクのコメントがあります。 –

+0

@BobSnyder - ありがとうございました。あなたのコメントは非常に役に立ちました。私はその質問に対する答えを見つけることができた。そして、あなたは叩かれました。ポインタを気に入る –

答えて

3

最近、FirebaseはSDKのサンプルコードとドキュメントが古くなっているため、SDKを更新したようです。関数を終了しようとしていても、ブール値を持つreturnを指定する必要があります。プロミスが返されない上記のコードのreturn statementsのそれぞれに対してreturn trueである必要があります。

Firebaseがサンプルコードとドキュメントを更新した後、この質問と回答を削除します。理由を知らずにこの問題につまずくかもしれない人々のためにここにそれを残すまで。

関連する問題