Skip to main content

.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

string | number
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.
function
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 node
  • key - The property key
  • message - The full message object
  • event - Event control object with .off() method
object
Options for query behavior

Return Value

Returns a new GUN chain reference pointing to the specified key. This allows method chaining.

Examples

Numeric Keys

Query with Callback

Error Handling

Implementation Details

From the source code (get.js:4-107):
The .get() method:
  1. Validates the key (non-empty string or number)
  2. Creates or retrieves a cached chain reference
  3. For callbacks, sets up query filtering on incoming data
  4. 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