.get()
The.get() method retrieves data from the graph. It can navigate to a specific node by key, or when passed a callback function, it becomes a query that filters data.
Signature
Parameters
The property key to navigate to. Can be a node soul (for root chains) or a property name (for nested chains).Note: Empty strings (
'') are not valid keys and will return an error.When provided as the first argument, acts as a filter/query function that processes incoming data.Signature:
function(data, key, message, event)data- The data at this nodekey- The property keymessage- The full message objectevent- Event control object with.off()method
Options for query behavior
Return Value
Returns a new GUN chain reference pointing to the specified key. This allows method chaining.Examples
Navigate by Key
Numeric Keys
Query with Callback
Error Handling
Implementation Details
From the source code (get.js:4-107):.get() method:
- Validates the key (non-empty string or number)
- Creates or retrieves a cached chain reference
- For callbacks, sets up query filtering on incoming data
- Returns a chainable reference for further operations
Caching Behavior
GUN caches chain references internally:- Multiple calls to
.get('key')return the same chain reference - Cached chains share the same data and listeners
- This improves performance and ensures consistency
Notes
.get()is synchronous and returns immediately- The actual data is loaded asynchronously
- Use
.on()or.once()to access the loaded data - You can chain multiple
.get()calls to traverse nested objects - When used with callbacks, it becomes a powerful query mechanism