将文件上传到 Firebase 存储时遇到问题

时间:2021-02-04 03:08:27

标签: javascript firebase web-scraping puppeteer

我目前正在学习 puppeteer 和 firebase。我想要做的是创建网页的 pdf 并上传到 firebase 存储。这是我的代码。

const puppeteer = require('puppeteer');
const fs = require('fs').promises;
const firebase = require('firebase');
require("firebase/storage");

const url = process.argv[2];
if (!url) {
    throw "Please provide URL as a first argument";
}

var firebaseConfig = {
        #Firebase Config Goes here
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);


#Function to generate PDF file
async function run () {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    //await page.goto(url);
    await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 60000} );

    //await page.pdf({ path: 'api.pdf', format: 'A4' })

    const myPdf = await page.pdf();
    
     await browser.close()

     return myPdf;
}

const myOutput = run();

#Upload to Firebase based on the instruction here https://firebase.google.com/docs/storage/web/upload-files
 
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
storageRef.child(myOutput).put(myOutput)

但是,我在执行代码时遇到了这个错误

$ node screenshot.js https://en.wikipedia.org/wiki/Aung_San_Suu_Kyi
C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1040
        .split('/')
         ^

TypeError: childPath.split is not a function
    at child (\Test\node_modules\@firebase\storage\dist\index.cjs.js:1040:10)
    at getChild (\Test\node_modules\@firebase\storage\dist\index.cjs.js:2610:19)
    at ReferenceCompat.child (Test\node_modules\@firebase\storage\dist\index.cjs.js:2833:25)
    at Object.<anonymous> (C:\Users\ppham\NucampFolder\Test\screenshot.js:55:12)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

这是说 childPath.split 不是一个函数。我对此很困惑,因为我已经安装了所有的 firebase 软件包。我一直在寻找这个错误一段时间,但到目前为止没有运气。有人知道如何解决这个问题吗?

================================================ ==================================

编辑#1:正如弗兰克在下面指出的那样,我必须将 storageRef.child(myOutput).put(myOutput) 更改为 storageRef.child("filename.pdf").put(myOutput) 之类的内容。它可以运行,但后来我遇到了这个错误。

$ node screenshot.js https://google.com
Promise { <pending> }
(node:20636) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'byteLength' of undefined
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:833:40
    at Array.forEach (<anonymous>)
    at Function.FbsBlob.getBlob (C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:832:25)
    at multipartUpload (C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1519:24)
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:2003:31
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1900:21
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:20636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这似乎暗示 myOutput 不包含任何内容。我以为我在运行函数中返回了pdf,它会传递给上传函数?我一直在阅读 Puppeteer 文档,但找不到任何不起作用的原因。有人知道为什么这是无效的吗?

1 个答案:

答案 0 :(得分:1)

错误消息表明您传递给 child() 的值不是字符串。

既然你打电话:

storageRef.child(myOutput).put(myOutput)

那是有道理的。我的猜测是您想将文件名传递给 child(),例如:

storageRef.child("filename.pdf").put(myOutput)