5

I've started using GunDB and I'm really enjoying it. I easily create complicated relationships without many problems associated with relational or document databases.

Unfortunately, I am having problems with a seemingly simple problem.

I've created nodes successfully, but later want to get the keys so I can embed them on the page. For example, on one page I am adding users with a form. Then, on another page, I want to get the list of users and create a select input that lists their names as the text, and keys as the values.

For example, on page one I have something like -

var user1 = gun.get('user/1').put({name: user1});
var user1 = gun.get('user/2').put({name: user2});
var users = gun.get('users');
users.set(user1);
users.set(user2);

One the other page, I have something like -

var users = gun.get('users');

users.map().val(function(user) {
  var userOption = document.createElement("option");
  userOption.text = user.name;
  userOption.value = user.key; // for example, though this does not work
  userSelect.appendChild(userOption);
  
});

Later, I want to use the option values in something like -

var user = gun.get(selectedUserOption.value);

Unfortunately, I can't work out how to get the key. It seems to be saved in the node object as "#", but I can't determine how to access this value.

user12700
  • 63
  • 4

1 Answers1

6

Great question! The 2nd parameter on most callbacks have the field or key of the data you are trying to get. From your example:

users.map().val(function(user, ID) {
  var userOption = document.createElement("option");
  userOption.text = user.name;
  userOption.value = ID; // for example, this now should work
  userSelect.appendChild(userOption);
});

Now you can do users.path(selectedUserOption.value) to grab the same user reference / chain context!

I am glad to hear you are enjoying using gun :) I'd love to see a demo of your project! Mind sharing?

marknadal
  • 7,534
  • 4
  • 25
  • 22
  • 2
    Mark, thanks for the quick response, and thanks also for creating and sharing GunDB! I'd be happy to share my project, but it's still early days with just a couple of forms on localhost. I've tried using ID as the second parameter. It works when using `users.map(function(user, ID))` but not when using `users.map().val(function(user, ID))`. I've used http://jsbin.com/ziremuquro/edit?js,console to test. – user12700 Jan 15 '17 at 22:36
  • 2
    I still don't understand GunDB that well, so I'm struggling a bit. If I use val(), I can't see the ID. If I use map(), I get duplicates. However, I managed a hack solution. To get the IDs of the users, I did the following: `var users = gun.get('users'); users.map().val(function(user) {console.log(user._['#'];});` It's not pretty, and I'm sure there are implications, but it's working... – user12700 Jan 16 '17 at 12:07
  • 2
    Oh indeed, that is a bug - thank you for pointing that out. Your hack for now will work, but I will either try to update the answer, publish a fix, or something else. It might be a week or so though - really busy this week. Jump on the https://gitter.im/amark/gun there are a bunch of people who can help there as well (although making sure to also post the Questions to StackOverflow is preferred). – marknadal Jan 17 '17 at 20:24
  • Hi @marknadal any update in this one. Thank you – Bitfinicon Jul 15 '22 at 15:30