Home > Software design >  JS: Cannot search for two parameters - async gives back only the first parameter
JS: Cannot search for two parameters - async gives back only the first parameter

Time:01-27

I am trying for a day now and cannot solve this problem: I have a website where I can add contacts to a SQLite database. I want to query the database by studentID or nachname (this means last name). I have an API endpoint which seems to be working and a getContact function which only works for the first parameter. In this case it only searches for studentID and if I change the order I can only get my data by nachname. For now I just want to display the response simply at the URL localhost:8000/api/contacts/studentID or /nachname to see the json response.

I tried this getContact function:

async getContact(**studentID, nachname**) { //here only the first parameter works
    let result = [];
    let query;
    let params = [];
    if (studentID && !nachname) {
      query = "SELECT * FROM contacts WHERE studentID = ?";
      params.push(studentID);
    } else if (nachname && !studentID) {
      query = "SELECT * FROM contacts WHERE nachname = ?";
      params.push(nachname);
    } else if (studentID && nachname) {
      query = "SELECT * FROM contacts WHERE studentID = ? OR nachname = ?";
      params.push(studentID, nachname);
    }
    result = await new Promise((resolve, reject) => {
      db.all(query, params, (error, row) => {
        if (error) {
          reject(error);
        } else {
          resolve(row);
        }
      });
    });
    return result;
  }

My API endpoint (setup with Express.js in Node.js) looks like this currently:

app
  .get("/api/contacts/:studentID?/:nachname?", async (request, response) => {
    const studentID = request.params.studentID;
    const nachname = request.params.nachname;
    console.log(request.params.studentID);
    console.log(request.params.nachname);

    const contact = await contactsManager.getContact(studentID, nachname);

    if (contact) {
      response.status(200).send(contact);
    } else {
      response.status(404);
    }
  })

I don't understand why the getContact function only works with the first parameter.

One strange thing I recognized: Right now I could search for localhost:8000/api/contacts/1 and would see the right entry, and when I add .../contacts/1/Behrens I see the entry with the ID 1 and also the entries of the people named Behrens. Maybe this information helps?

CodePudding user response:

The issue is that in your getContact function, you are using the ** operator in front of studentID and nachname, which is causing them to be treated as keyword arguments instead of regular arguments. As a result, the function is only able to identify and use the first argument passed to it, while the second argument is ignored.

To fix this, you should remove the ** operator from the function definition so that the function takes in two regular arguments:

async getContact(studentID, nachname) {
  // ...
}

Also, you can use SELECT * FROM contacts WHERE studentID = ? AND nachname = ? instead of SELECT * FROM contacts WHERE studentID = ? OR nachname = ? if you want to check the both parameter are exist in the database.

Also, you should check for the existence of the parameters in the API endpoint before passing them to the getContact function, otherwise, you may end up passing undefined values to the function which will cause an error.

app.get("/api/contacts/:studentID?/:nachname?", async (request, response) => {
let studentID = request.params.studentID;
let nachname = request.params.nachname;
if(!studentID) studentID = null;
if(!nachname) nachname = null;
console.log(studentID);
console.log(nachname);

const contact = await contactsManager.getContact(studentID, nachname);

if (contact) {
  response.status(200).send(contact);
} else {
  response.status(404);
}
})

Also, you can use only one parameter in the endpoint and use if-else statements to check which parameter is passed and then act accordingly in the function.

app.get("/api/contacts/:param?", async (request, response) => {
let studentID = request.params.param;
let nachname = request.params.param;
if(studentID.match(/^\d $/)){
    studentID = request.params.param;
    nachname = null;
}else{
    studentID = null;
    nachname = request.params.param;
}
console.log(studentID);
console.log(nachname);

const contact = await contactsManager.getContact(studentID, nachname);

if (contact) {
  response.status(200).send(contact);
} else {
  response.status(404);
}
})

This should fix the issue with the getContact function only working with the first parameter, and allow you to query the database by both studentID and nachname.

  • Related