Stuck at Read Multiple Keys

heres the git hub repo

the error message is

 Keys
[your_program] [ 'apple' ]
[your_program] KEYS * Response: *1
[your_program] $5
[your_program] apple
[your_program] 
[tester::#JW4] Received: "*1\r\n$2\r\napple,apple\r\n*1\r\n$5\r\na"
[tester::#JW4]                          ^ error
[tester::#JW4] Error: Expected \r\n after 2 bytes of data in bulk string
[tester::#JW4] Test failed

the code block

      if (commands[2] === "KEYS" && commands[4] === "*") {
        console.log("******** Handling KEYS * Command ********");

        // Get all keys, excluding config entries
        const keys = Array.from(dataStore.keys()).filter(
          (key) => key !== "dir" && key !== "dbfilename"
        );

        console.log("Keys");
        console.log(keys);
        let response = `*${keys.length}\r\n`; // RESP array of keys
        keys.forEach((key) => {
          // Properly format each key as a bulk string
          console.log("Key are");
          console.log(key);
          response += `$${key.length}\r\n${key}\r\n`; // Each key is a bulk string
          console.log("Key: " + key);
        });
        

        console.log("KEYS * Response:", response);
        connection.write(response); // Send the response to the connection
      }
    }

@rafterjob1 what part of the error message do you find confusing?

I am just iterating the array , how did i get the comma in between
can you help me solve it , i have stuck in this stage for very longtime

Looks like you’ve got two connection.write calls - are both required?

Line 96 and line 121.

looks like you have two $ signs in this line
response += `$${key.length}\r\n${key}\r\n`; you might need to escape the character

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.