Javascript 将文件上载到firebase存储时出现问题
我现在正在学习木偶演员和火堆。我试图做的是创建一个网页的pdf并上传到firebase存储。这是我的密码Javascript 将文件上载到firebase存储时出现问题,javascript,firebase,web-scraping,puppeteer,Javascript,Firebase,Web Scraping,Puppeteer,我现在正在学习木偶演员和火堆。我试图做的是创建一个网页的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) {
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
$node screenshot.jshttps://en.wikipedia.org/wiki/Aung_San_Suu_Kyi
C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:1040
.split(“/”)
^
TypeError:childPath.split不是函数
在子级(\Test\node\u modules\@firebase\storage\dist\index.cjs.js:1040:10)
在getChild(\Test\node\u modules\@firebase\storage\dist\index.cjs.js:2610:19)
在ReferenceCompat.child(Test\node\u modules\@firebase\storage\dist\index.cjs.js:2833:25)
反对。(C:\Users\ppham\NucampFolder\Test\screenshot.js:55:12)
at模块编译(内部/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:947:10)
在Module.load(内部/modules/cjs/loader.js:790:32)
at Function.Module._load(内部/modules/cjs/loader.js:703:12)
位于Function.Module.runMain(internal/modules/cjs/loader.js:999:10)
在internal/main/run\u main\u 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.
$node screenshot.jshttps://google.com
承诺{}
(节点:20636)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“ByTeleLength”
在C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:833:40
在Array.forEach()处
位于Function.FbsBlob.getBlob(C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:832:25)
多端口加载(C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:1519:24)
在C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:2003:31
在C:\Users\ppham\NucampFolder\Test\node\u modules\@firebase\storage\dist\index.cjs.js:1900:21
在处理和拒绝时(内部/process/task_queues.js:85:5)
(节点:20636)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:1)
(节点:20636)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
这似乎暗示myOutput不包含任何内容。我想我已经在run函数中返回了pdf,它将被传递到upload函数?我一直在阅读木偶演员的文档,但找不到任何理由说明这不起作用。有人知道为什么这是无效的吗?错误消息说您传递给
child()
的值不是字符串
既然你在打电话:
storageRef.child(myOutput).put(myOutput)
那是有道理的。我猜您希望将文件名传递到child()
,例如:
storageRef.child("filename.pdf").put(myOutput)
非常感谢。这似乎就是原因。但我后来遇到了这个问题。我会用它更新帖子。如果你在做这个更改后有新的/不同的问题,请打开一个新问题。谢谢Frank。我在这里提出了一个新问题-