typescript 检查可观察值

osh3o9ms  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(152)

我只是在学习Typescript/nestjs,所以整个订阅的概念仍然有点模糊。
我试图检查是否有一个传递的ID存在于我们的数据库中,如果有,那么我知道这个人是一个代理,否则他们是一个客户。

getAgent(agentId: number) {
    const agent$ = from(
        this.agentRepository.findAll({
            attributes: ['agentId'],
            where: {
                agentId: {
                    [Op.eq]: agentId
                }
            }
        })
    );
    return agent$;
}

从端点传回。

[
  {
    "agentId": 1234567890
  }
]

我在if语句中使用它

if (event.author_id === -1) {
    update_actor = 'system';
    source_status = 0;
} else if (this.getAgent(event.author_id)) {
  console.log(this.getAgent(event.author_id));

  update_actor = 'agent';
} else {
update_actor = 'customer';
source_status = 1;
}

如果我添加subscribe(),它将从端点返回:

{
    "closed": false,
    "_parentage": null,
    "_finalizers": null,
    "isStopped": false,
    "destination": {
    "partialObserver": {}
    }
  }

如果我使用它与getValue我得到属性'getValue'不存在类型'Observable〈Agent[]〉'。
我也试探着:

const foundAgentId = agent.pipe(
      map(async (agent) => agent.map(({ agentId }) => agentId))
   );

它通过端点击中它而返回。

[ 1234567890 ]

如果没有该id记录,则为[]。
我的第一个问题是,如何让它返回true/false?
我的第二个问题是,如果返回完整的代理列表,如何执行agentList.includes(123456780)之类的操作?
提前感谢!

ff29svar

ff29svar1#

谢谢@MoxxiManagarm最终做到了这一点

const fetchAgentList = await this.getAgents();
    const agentList = fetchAgentList.map((agent) => {
        return agent.agentId;
    });

相关问题