首页 > 解决方案 > Firebase 函数在尝试写入存储时抛出错误

问题描述

我有一个 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.jsfirebasegoogle-cloud-functionsgoogle-cloud-storage

解决方案


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

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


推荐阅读