如何使用Nodejs客户端获取TypeDB中实体的值?

64jmpszr  于 2023-04-29  发布在  Node.js
关注(0)|答案(2)|浏览(114)

我尝试在nodejs客户端中获取实体属性的实际值,但只得到字符串Ids。我尝试了许多不同的版本。getValue()或asAttribute或getAttribute,但只获取错误消息。如何检索属性的实际值?
这是我的代码

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;
    }```
2w3kk1z5

2w3kk1z51#

我相信你应该使用.map.value,像这样:

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

根据https://docs.vaticle.com/docs/concept-api/thing#retrieve-value-(local)(确保在该页面上将语法切换为javascript)。

lc8prwob

lc8prwob2#

您可以使用概念API,正如弗拉基米尔指出的那样。我们还在所有客户端中发布了JSON输出,因此您应该能够在概念图上调用.toJSONRecord(),输出将包含概念图中包含的所有信息。
编辑:确保您使用的是客户端版本2。17或更晚

相关问题