Get Counter Values



Get a single Counter's value

Get usage:

  • Open a session
  • Create an instance of countersFor.
  • Call countersFor.get to retrieve the current value of a single Counter.

Get example:

// 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 syntax:

get(counter);
Parameter Type Description
counter string Counter's name
Return Type Description
Promise<number> A Promise resolving to the Counter's current value, or to null if counter doesn't exist.

Get all Counters of a document

GetAll usage:

  • Open a session.
  • Create an instance of countersFor.
  • Call countersFor.getAll to retrieve the names and values of all counters associated with the document.

GetAll example:

// 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]);
}

GetAll syntax:

getAll(counters);
Return Type Description
Promise<object> A Promise resolving to a dictionary of counter values by counter names