【问题标题】:Error "TypeError: Cannot read property 'indexOf' of undefined" when using ipfs-api使用 ipfs-api 时出现错误“TypeError:无法读取未定义的属性 'indexOf'”
【发布时间】:2018-10-26 19:06:39
【问题描述】:

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

这是错误描述

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;
      }) 
    },

【问题讨论】:

    标签: javascript vue.js request ipfs


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的建议。我注意到从您的销售点获取环境设置有问题。原因是“process.env.IPFSPROTCOL”获取,设置文件获取不充分。
    • 我还注意到拼写错误!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多