You can read data from deeply nested objects using path queries.
let cid = await ipfs.dag.put({
my: {
deep: {
obj: 'is cool'
}
}
})
console.log(await ipfs.dag.get(cid, {
path: '/my/deep/obj'
}))
// prints { value: 'is cool', remainderPath: '' }
ipfs.dag.get
allows queries using IPFS paths and returns decoded blocks we refer to as nodes. The return value is an object containing the value of the query and any remaining path that was unresolved.
The cool thing about this API is that it can also traverse through links.
let cid = await ipfs.dag.put({ foo: 'bar' })
let cid2 = await ipfs.dag.put({
my: {
other: cid
}
})
console.log(await ipfs.dag.get(cid2, {
path: '/my/other/foo'
}))
// prints { value: 'bar', remainderPath: '' }
Gotcha! Notice above how this method returns not the value itself, but rather an object that contains a value
property. You can't access that value
property until the promise completes, a problem which can be solved in two ways:
// Option 1: Wrap your await statement in parentheses to let the promise complete
return (await ipfs.dag.get(cid2, {
path: '/my/other/foo'
})).value
// Option 2: Save the result to a variable and then access its value
let node = await ipfs.dag.get(cid2, {
path: '/my/other/foo'
})
return node.value
Use ipfs.dag.get
to return the value of test
by traversing the link from the object you put
in the previous challenge. (Hint: Be sure to access the value
property only after the promise completes.)
Feeling stuck? We'd love to hear what's confusing so we can improve this lesson. Please share your questions and feedback.