【问题标题】:Firebase functions throwing error when attempting to write to StorageFirebase 函数在尝试写入存储时抛出错误
【发布时间】:2020-08-19 03:31:40
【问题描述】:

我有一个 firebase 函数,它尝试将数据写入 firebase 存储:

const bucket = admin.storage().bucket(/*removed for this question*/);
var tempJSONObject = {
 testing: "why are we testing",
 anothertest:"constanttesting"
}
try{
const fileName = `storedjokes/81.json`
const file = bucket.file(fileName);
const writeStream = fs.createWriteStream(file);
writeStream.write(tempJSONObject,(err) => {
 if(err){
   console.log(err.message);
 }else{
   console.log("data written");
 }
});
}catch(e){
 console.log('error',e);
}

我在 Firebase 日志中收到一条错误消息:

error TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 URL 类型之一。接收到的类型对象

我该如何解决这个问题?

【问题讨论】:

    标签: node.js firebase google-cloud-functions google-cloud-storage


    【解决方案1】:

    错误消息非常明确:您只能将字符串(或文件的路径)、缓冲区或 URL 写入写入流。

    如果要将 JSON 对象的文字字符串表示形式写入文件,则必须使用 JSON.stringify(tempJSONObject) 将对象转换为字符串。

    【讨论】:

    • 我做了: var stringy = JSON.stringify(tempJSONObject);并通过了它而不是 tempJSONObject 但我仍然得到同样的错误。
    猜你喜欢
    • 2021-05-09
    • 2021-12-09
    • 2017-01-11
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2023-03-19
    • 2019-09-29
    相关资源
    最近更新 更多