I am performing the following query with NodeJS on an Oracle SQL database
export async function getGuard(req: Request, res: Response): Promise<any> {
const conn = await connect();
let { documento} = req.params;
const result = await conn.execute(
`
select ideide
from basdat.siide, basdat.siideead
where idecod='${documento}'
and idecod=ideeadcod
and ideeadact='S'
`
);
console.log(result.rows[0][0].length);
if (result.rows[0][0].length > 0) {
return res.json(result.rows[0][0]);
} else {
res.status(404).json({ text: "Registro no existe" });
}
}
And I am trying to validate that if the record that I am sending does not exist, I can generate an action in a Record form in Angular
this.documentoreg = this.forma.value.documento
console.log(this.documentoreg);
this._usuarioService.getdocumento(this.forma.value.documento)
.subscribe( resp => {
this.documentoser = resp;
console.log('documentode usuario servinte',resp)
if (this.documentoreg == this.documentoser) {
console.log('si son iguales');
} else {
console.log('No son iguales');
}
}, err => {
console.log(err);
}
);
When the record exists, it executes the IF, but when I pass it a document that does not exist, it does not return anything, it does not even execute the IF
PS: I only use the Oracle database to check if the document exists, I am not storing anything in it
I see a couple of details in the way you're working on the server side.
First of all, you have a function type
async
. In it you use the wordawait
to make the connection and to execute the query.If you're using
node-oracledb
(you don't specify it, but I imagine you do), you must wrap your code inside atry/catch
. Since by not doing so you would not be capturing a possible connection error or an error in the query.Being a REST service, these errors must be indicated to the client using a status
HTTP 5xx
, since they are Server errors. The message you send to the client depends on what you want to expose from your API. I recommend custom messages if your API is public, since thedriver
Oracle one could show details of your Database schema if you send the message directly to the client.Second, as @PabloLozano indicates, if the response sent to the client includes a status
404
, then the client will not receive a valid response (HTTP 2xx) from your API.One way around this, if you want to maintain state
404
to indicate a resource not found (which is not exactly what happens), is to use an interceptor, as @PabloLozano suggests.Another way to do it would be to not send the state
404
to the client when the query returns no results. Instead, you send a response to the client (usually an empty object or aarray
void), and let the client interpret the returned result.Since in your client you are already testing or comparing objects, then the ideal would be to send an empty object.
Your server side code could look like this:
In this way, when you receive a response in your client, you will have control over the result of the query you receive, having the following cases:
HTTP 500
.HTTP 200
, which includes:Obviously, if the query returns no results (or returns a
objeto/array
blank), it means that the record has not been found in the Database.In this way you separate your type states
404
that normally refer to a network resource not found, from the type errors500
that occur when the Server has an internal failure (could not connect to the DB or the query to the DB returned a mistake).If the connection and query to the DB (internal process of your server) are successful, then it is not necessary to return a status other than
200
, since:endpoint
) is accessible.I hope this clarifies your doubts a little more.
If the response has a 404 status, it's considered an error and the function passed to subscribe won't be executed, instead you'll need to catch it with
catchError
, something like :Although you could have a global interceptor that is responsible throughout the application for capturing all HTTP errors centrally , so that you do not have to repeat this code in each service.