Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] how can one find the root node of a given user? #1326

Open
rozek opened this issue May 16, 2023 · 3 comments
Open

[Doc] how can one find the root node of a given user? #1326

rozek opened this issue May 16, 2023 · 3 comments

Comments

@rozek
Copy link

rozek commented May 16, 2023

Well, it's really difficult to find the relevant bits of information in the docs - sorting out all wrong or outdated parts and collecting everything else in order to combine it to s.th. useful...

Right now, I'm trying to find the root node of a user given by an alias. This is my current unerstanding:

  const Gun     = new GUN()
  const GunUser = Gun.user() // creates an environment for an anonymous user

/**** find Alice's root node ****/

  let Alice = await (async () => {
    return new Promise((resolve,reject) => {
      Gun.get('~@alice').once((Data) => {
        for (const Key in Data) {
          if ((Key !== '_') && Data.hasOwnProperty(Key)) {
            let Value = Data[Key]
            if (
              (typeof Value === 'object') && (Value != null) &&
              (Value['#'] === Key)
            ) { return resolve(Gun.user(Key.slice(1))) }
          }
        }
        reject('alias not found')
      })
    })
  })()

  console.log('Alice\'s root node',Alice) // shows s.th.
  console.log('Alice\'s address',Alice.get('address')) // fails with "Alice.get is not a function"

This gives me s.th. which looks like the contents of Alice's root node - but I cannot .get or .put from there.

So: how do I find a user's root node in order to inspect any public information found there?

@rozek
Copy link
Author

rozek commented May 16, 2023

Very, very strange...

If I change the above code to

  const Gun     = new GUN()
  const GunUser = Gun.user() // creates an environment for an anonymous user

/**** find Alice's root node ****/

  let AliceKey = await (async () => {
    return new Promise((resolve,reject) => {
      Gun.get('~@alice').once((Data) => {
        for (const Key in Data) {
          if ((Key !== '_') && Data.hasOwnProperty(Key)) {
            let Value = Data[Key]
            if (
              (typeof Value === 'object') && (Value != null) &&
              (Value['#'] === Key)
            ) { return resolve(Key.slice(1)) }
          }
        }
        return reject('alias not found')
      })
    })
  })()

  let Alice = Gun.user(AliceKey)

  console.log('Alice\'s root node',Alice)
  console.log('Alice\'s address',Alice.get('address'))

then everything works as foreseen...

Does anybody know why I can't resolve with a GUN node?

@gustavcorpas
Copy link

Bit late here, but perhaps this has to do with how nodes can be awaited:
see docs here

in short:

gun.get("some-key-here") is not the same as await gun.get("some-key-here"). Awaiting it will give you the value of the node. If you wish to return the actual gun chain, you can wrap it in an object like so:

async () => {
  const node = gun.get("some-key-here");
  return {node}; // we must wrap so that awaiting it will not cause us to promisify the node.
}

@trPrince
Copy link

If you have the user's username and password, you can authenticate first using GunUser.auth(username, password) And then get or put on that user's node using GunUser.get or GunUser.put. In short, gun.user() works as the root node for the current (logged in/authenticated) user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants