Why is my RethinkDB getAgentById query from Express Server gets undefined error?

20 Views Asked by At

I am fairly new to the software development world. Trying to explore technologies, if Im not using the correct terminology for the problem bear with me.

I am having a problem with a query from Express.js to RethinkDB.I am getting the agentID value correctly from a rabbitmq server as you can see it logs fine. But getAgentById query returns a undefined error as you can see from the output. What could be the reason?

channel.consume(queueName, async (msg) => {
        const agentId = msg.content.toString();
        try {
          if (agentId) {
            console.log(agentId)
            const existingAgent = await agentsModel.getAgentById({agentId});
            console.log(existingAgent)
            if (!existingAgent) {
              await agentsModel.createAgent({ uuid: agentId });
              console.log(`Agent ${agentId} created successfully`);
            } else {
              console.log(`Agent ${agentId} already exists in the database.`);
            }
            channel.ack(msg);
          } else {
            console.error("Agent ID is undefined or empty.");
          }
        } catch (error) {
          console.error(`Error creating/updating agent ${agentId}: ${error}`);
        }
      });

db model function

const getAgentById = async (connection, agentId) => {
  try {
    const result = await r.db('DBname').table('agents').get(agentId).run(connection);
    return result;
  } catch (error) {
    throw error;
  }
};

Output

Express API Server listening on 3001
32c15ea4-dc76-43a6-ac30-928455bde275
Error creating/updating agent 32c15ea4-dc76-43a6-ac30-928455bde275: ReqlDriverCompileError: Argument 1 to get may not be `undefined`.

I look around a bit on internet and tried chatgpt prompts but didnt helped.

0

There are 0 best solutions below