Get Counter Values
-
Use
countersFor.get
to retrieve the value of a single Counter,
orcountersFor.getAll
to retrieve the names and values of all Counters associated with a document. -
For all other
countersFor
methods see this Overview. -
In this page:
Get a single Counter's value
- Open a session
-
Create an instance of
countersFor
.- Either pass
countersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.query or from session.load.
- Either pass
- Call
countersFor.get
to retrieve the current value of a single Counter.
// Open a session
const session = documentStore.openSession();
// Pass a document ID to the countersFor constructor
const documentCounters = session.countersFor("products/1-A");
// Call `get` to retrieve a Counter's value
const daysLeft = await documentCounters.get("DaysLeftForSale");
console.log("Days Left For Sale: " + daysLeft);
Get all Counters of a document
- Open a session.
-
Create an instance of
countersFor
.- Either pass
countersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.query or from session.load.
- Either pass
- Call
countersFor.getAll
to retrieve the names and values of all counters associated with the document.
// Open a session
const session = documentStore.openSession();
// Pass a document ID to the countersFor constructor
const documentCounters = session.countersFor("products/1-A");
// Call `getAll` to retrieve all of the document's Counters' names and values
const allCounters = await documentCounters.getAll();
for (var counter in allCounters) {
console.log("counter name: " + counter + ", counter value: " + allCounters[counter]);
}