【问题标题】:Unhandled Rejection (TypeError): fs.createReadStream is not a function. Error at the time of image uploading to ipfs( pinata)未处理的拒绝 (TypeError):fs.createReadStream 不是函数。图片上传到 ipfs(pinata) 时出错
【发布时间】:2021-12-18 19:15:10
【问题描述】:

我试图从我的 react js 前端应用程序将图像上传到 pinata ipfs 云。但它向我显示错误:Unhandled Rejection (TypeError): fs.createReadStream is not a function。 .

这是我的代码 sn-p:

require('dotenv').config();
const fs = require('fs');

const axios = require('axios');
const FormData = require('form-data');

const key = process.env.REACT_APP_PINATA_KEY;
const secret = process.env.REACT_APP_PINATA_SECRET;

export const pinFileToIPFS = async (filePath) => {
const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
//we gather a local file for this example, but any valid readStream source will work here.
let data = new FormData();
data.append('file',fs.createReadStream(filePath));

return axios.post(url,
    data,
    {
        maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
        headers: {
            'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
            'pinata_api_key': key,
            'pinata_secret_api_key': secret
        }
  })
  .then(function (response) {
      //handle response here
      return {
        success: true,
        pinataUrl: "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash
    };
  })
  .catch(function (error) {
      //handle error here
      console.log(error)
        return {
            success: false,
            message: error.message,
        }
  });
};

如何解决它或者有什么方法可以做到这一点而不是使用'fs'?

【问题讨论】:

    标签: reactjs ipfs nft


    【解决方案1】:
    1. 'fs' 包已被视为恶意软件,来自github security placeholder 页面:“此包包含恶意代码,已被 npm 安全团队从注册表中删除...”。我不建议使用它。
    2. 错误消息指定(TypeError): fs.createReadStream is not a function.
    3. 在研究过程中,我发现fs-extra npm 包是fs 的一个很好的替代品。

    使用 fs-extra,您可以使用 readJson(许多 methods 之一)方法通过 callbackpromisesasync/await 执行它,例如:

    const fse = require('fs-extra');
    
    ...
    
    try {
        const fileObject = await fse.readJson(filePath);
        data.append('file', fileObject);
        console.log(fileObject);
    } catch (err) {
        console.error(err);
    }
    
    ...
    

    如果这不起作用,请根据此node.js example 尝试以下操作:

    1. 使用 createReadStream 方法创建变量:
     // This line opens the file as a readable stream
    let readStream = fs.createReadStream(filePath);
    
    1. 按照预期使用以下内容来检索响应并捕获任何错误
      // This will wait until we know the readable stream is actually valid before piping
      readStream.on('open', function () {
        // This just pipes the read stream to the response object (which goes to the client)
        readStream.pipe(res);
        // in your case:
        data.append('file', res);
      });
    
      // This catches any errors that happen while creating the readable stream (usually invalid names)
      readStream.on('error', function(err) {
        res.end(err);
      });
    

    【讨论】:

    • 感谢您的回答。但是根据你的修复后它显示: TypeError: Object prototype may only be an Object or null: undefined (anonymous function) node_modules/graceful-fs/polyfills.js:139 136 | 137 | 138 | // 这确保了 util.promisify 像原生 fs.read 一样工作。 > 139 | if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read) | ^ 140 |返回阅读 141 | })(fs.read) 142 | .......
    • 'fs' 包已被视为恶意软件 -- npm 上的fs 包是恶意的,是的,但是require('fs') ALWAYS 导入 Node.js 内置fs 模块。顺便说一句,fs-extras 只是在fs 上添加了一些东西,所以如果你使用fs-extras,你就是在使用fs
    • @user17242583 我并没有完全意识到这一点,只是注意到有关恶意软件的警告消息,虽然这不是一个好主意,但你提出了一个好观点。 arefin:我将尽我所能更新我的答案,回到“fs”。
    猜你喜欢
    • 2018-12-09
    • 2021-10-14
    • 2020-11-16
    • 2020-05-06
    • 2019-10-27
    • 2020-09-26
    • 2021-04-16
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多