Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

ipfs.dag.get error #3854

Closed
longren610 opened this issue Sep 7, 2021 · 4 comments
Closed

ipfs.dag.get error #3854

longren610 opened this issue Sep 7, 2021 · 4 comments
Labels
need/triage Needs initial labeling and prioritization

Comments

@longren610
Copy link

Regarding the BUG in ipfs.dag.get, the sample code in the ipfs.dag.get part of https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/DAG.md cannot be run normally.

The following code can run normally and get the hash value: bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq

const { create } = require('ipfs-http-client')
const CID = require('cids');

const ipfs = create({ host: '127.0.0.1', port: '5001', protocol: 'http' })

var cid = async function(obj) {
    return await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' });
}

const obj = {
  a: 1,
  b: [1, 2, 3],
  c: {
    ca: [5, 6, 7],
    cb: 'foo'
  }
}

cid(obj).then((ret) => {
  console.log(ret.toString())
});

image

Then use the hash value obtained and use the code in the example to get the data, and an error will be reported: No codec found for "undefined".code show as below:

const { create } = require('ipfs-http-client')
const CID = require('cids');

const ipfs = create({ host: '127.0.0.1', port: '5001', protocol: 'http' })

async function getAndLog(cid, path) {
  const result = await ipfs.dag.get(cid, { path:path })
}

getAndLog('bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq', '/a').then((ret) => {
  console.log(ret)
});

image

I tried several times and found that it was a cid problem. After modifying the code, I tried to run again, no more errors were reported, but the output value is always: undefined.code show as below:

const { create } = require('ipfs-http-client')
const CID = require('cids');

const ipfs = create({ host: '127.0.0.1', port: '5001', protocol: 'http' })

async function getAndLog(cid, path) {
  const result = await ipfs.dag.get(new CID(cid), { path:path })
}

getAndLog('bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq', 'a').then((ret) => {
  console.log(ret)
});

image

Use the cli command directly to obtain the data, and the data can be obtained normally.
image

Why is this so?

@longren610 longren610 added the need/triage Needs initial labeling and prioritization label Sep 7, 2021
@welcome
Copy link

welcome bot commented Sep 7, 2021

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additional round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

  • "Priority" labels will show how urgent this is for the team.
  • "Status" labels will show if this is ready to be worked on, blocked, or in progress.
  • "Need" labels will indicate if additional input or analysis is required.

Finally, remember to use https://discuss.ipfs.io if you just need general support.

@achingbrain
Copy link
Member

The cids module is deprecated, please use the CID class exported by the multiformats module:

const { create } = require('ipfs-http-client')
const { CID } = require('multiformats/cid');

const ipfs = create({ host: '127.0.0.1', port: '5001', protocol: 'http' })

async function getAndLog(cid, path) {
  const result = await ipfs.dag.get(CID.parse(cid), { path })
}

getAndLog('bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq', 'a').then((ret) => {
  console.log(ret)
});

@longren610
Copy link
Author

The cids module is deprecated, please use the CID class exported by the multiformats module:

const { create } = require('ipfs-http-client')
const { CID } = require('multiformats/cid');

const ipfs = create({ host: '127.0.0.1', port: '5001', protocol: 'http' })

async function getAndLog(cid, path) {
  const result = await ipfs.dag.get(CID.parse(cid), { path })
}

getAndLog('bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq', 'a').then((ret) => {
  console.log(ret)
});

Thanks for the answer, the reason is found, there is no return value in the getAndLog function, so "undefined" is always printed. Sorry to bother you.

@longren610
Copy link
Author

The reason is found, there is no return value in the getAndLog function, so "undefined" is always printed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need/triage Needs initial labeling and prioritization
Projects
None yet
Development

No branches or pull requests

2 participants