Error "TypeError: Cannot read property 'indexOf' of undefined" when using ipfs-api

Error "TypeError: Cannot read property 'indexOf' of undefined" when using ipfs-api

我正在使用 ipfs 进行开发 - api,我遇到了以下错误,将图像文件添加到 ipfs 节点无法正常工作。 查看错误的详细信息,似乎协议在 if (protocol.indexOf ('https') === 0) { in request.js.

中被视为未定义

这是错误描述

Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined
    at webpackJsonp../node_modules/ipfs-api/src/utils/request.js.module.exports (request.js:7)
    at requestAPI (send-request.js:165)
    at send (send-request.js:196)
    at send-files-stream.js:99
    at Function.promisify (add.js:41)
    at index.js:32
    at Object.add (add.js:60)
    at VueComponent._callee$ (HaikuCompose.vue?0664:118)
    at tryCatch (runtime.js:62)
    at Generator.invoke [as _invoke] (runtime.js:296)

这是我写的代码

import IPFS from "ipfs-api"
const ipfsConf = { host: process.env.IPFSHOST, port: process.env.IPFSPORT, protocol: process.env.IPFSPROTCOL }
const ipfs = new IPFS(ipfsConf)

export default {
  name: 'ipfstest',
  data() {
    return {
      file:null,
      buffer:null,
      ipfsHash:null,
    }
  },
  methods: {
    async addipfs() {
      await ipfs.add(this.buffer, (err, ipfsHash) => {
        console.log(err,ipfsHash);
        this.ipfsHash = ipfsHash[0].hash;
      }) 
    },

从模块源代码中,line 7 of the request.js file 上的 indexOf 用于存储协议的变量,在您的情况下是 undefined

根据您的代码,我想我可以安全地假设您的环境变量 process.env.IPFSPROTCOL 未定义。

TL:DR:我想你想写 IPFSPROTOCOL 而不是 IPFSPROTCOL