0

In the following code :

app.use('/Catalog', function(req, res)
  {                                                                                                             
        let query = "select id, name from Category where id = ?";
        let queryError = "select * from Category";
        db.all(query, [req.query.id], (err, rows) => {
                if(err == null){
                        res.writeHead(200, { 'Content-Type': 'text/plain'});
                        res.write(JSON.stringify(rows));
                        res.end();
                }
                else{
                        console.log(err);
                        db.all(queryError, (err, rows) => {
                                res.writeHead(200, { 'Content-Type': 'text/plain'});
                                res.write(JSON.stringify(rows));
                                res.end();
                        });
                }
        });

  });

It seems that the else condition is not working. Here are the rows of my Category table :

1|Motorcycles
2|Classic Cars
3|Trucks and Buses
4|Vintage Cars
5|Planes
6|Ships
7|Trains

The wanted behavior is that, if I ask for an id that is not in the table (/catalog?id=8 for example), the server should return all the categories available. But here, instead of returning all the categories, it just returns

[]

1 Answer 1

1

You are testing if there is an error.

The database is successfully returning all zero matching rows.

Errors are things like "The network connection to the database failed", "Requesting a column that does not exist" or a syntax error in the SQL.

Test if rows.length is 0 (assuming rows is an array).

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I thought that if I ask for something that does not exist, an error would be generated. But actually no, it returns an empty result, not an error. Thank you !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.