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.
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?