How can I get a value of an entity in TypeDB using Nodejs client?

98 views Asked by At

I try to get the actual values of entity attributes within a nodejs client but I only get the string Ids. I tried many different versions of .getValue() or asAttribute or getAttribute but just get error messages. How can I retrieve the actual values of attributes?

This is my code

export async function getMarketByTitle(title: string) {
  const database = "know_graph"; // Replace this with the name of your TypeDB database
  const { client, session, transaction } = await openTransaction(database);

  try {
    const query = `
      match
      $m isa market, has title "${title}", has background_information $background_information;
      $m has title $m_title;
      get $m, $m_title, $background_information;
    `;

    const iterator = await transaction.query.match(query);
    let market;
    for await (const answer of iterator) {
      const mConcept = answer.get("m");
      const mTitle = answer.get("m_title");
      const backgroundInformation = answer.get("background_information");
      const checkEntity = mConcept.isEntity();
      const checkAttribute = mTitle.isAttribute();

      market = {
        id: mConcept.toString(),
        title: msTitle.toString(),
        backgroundInformation: backgroundInformation.toString(),
        entity: checkEntity.toString(),
        attribute: checkAttribute.toString
      };

      return market;
    }```
2

There are 2 answers

0
Vladimir Izmalkov On

I believe you should use .map and .value, like that:

      const mTitle = answer.map.get("m_title").value;
      const backgroundInformation = answer.map.get("background_information").value;

As per https://typedb.com/docs/clients/2.x/node-js/node-js-api-ref.html#_retrieve_value_local (updated the link for the new docs URL!).

Can't comment on James's answer, so I'll add the link to the JSON method here: https://typedb.com/docs/clients/2.x/node-js/node-js-api-ref.html#_retrieve_a_concept_as_json

0
James Whiteside On

You can use the Concept API as Vladimir pointed out. We have also just released JSON output in all of our clients so you should be able to call .toJSONRecord() on your concept maps and the output will contain all the information contained in the concept map.

Edit: Make sure you're using client version 2.17 or later.