I just started out learning Node.js / Express and I still have difficulties with the Asynch functions. I made some functions to interact with a postgresql database (with some tutorials), and selecting rows from data is going fine but for some reason something is going from with deleting the rows. Here is an example of a function that is going well:
const getPlayers = () => {
return new Promise(function(resolve, reject) {
pool.query('SELECT * FROM Players ORDER BY p_id ASC', (error, results) => {
if (error) {
reject(error)
}
resolve(results.rows);
})
})
}
Now the following function is not going well. Console.log(id) gives the right number, but it seems that id is undefined when executing the query and I suspect that it has to do with Asynch/synch. Now Asynch is new for me, so I am also not an expert on what is going wrong. Here is the function that is nog going good:
const deletePlayer = (id) => {
return new Promise(function(resolve, reject) {
pool.query('DELETE FROM Players WHERE player_id = ?' , [id], (error,results) => {
if (error) {
reject(error)
}
resolve(`Player deleted with ID: ${id}`)
})
})
}
The function call:
app.delete('/laProjects/:id', (req, res) => {
players_model.deletePlayers(req.params.id)
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
Don't automatically assume it is an async issue. First try some simple console.log steps:
const deletePlayer = (id) => {
console.log("Started deletePlayer with id: ",id) ///////////
return new Promise(function(resolve, reject) {
console.log("Inside the promise, id is the same, namely: ",id) ///////////
pool.query('DELETE FROM Players WHERE player_id = ?' , [id], (error,results) => {
if (error) {
reject(error)
}
resolve(`Player deleted with ID: ${id}`)
})
})
}
If you don't see any console.log messages printed, maybe, as @steve16351 suggests, you are editing deletePlayer but actually calling another functiondeletePlayers?
When Promises first arrived in Javascript, they were the practical tool for asynchronous programming, so people sometimes used the term "async" for them in speech.
However since the async keyword arrived, it is better not to use the word "async" for things that are not that keyword.
Blocking code: a program that simply waits (preventing any other code running) while some external process happens.
Callbacks: the oldest form of asynchronous programming in JS. You tell JS to run a certain function only after some external event has happened.
Promises: a more convenient and readable way to achieve the same effect as callbacks. You can imagine the process to be constructed out of hidden callbacks.
async keyword: an even more convenient and readable way to achieve the same effects as callbacks and promises. It is implicitly made out of promises, although it protects you from having to think about how to construct a new promise.
This tells us that your original assumption, that the error is due to a variable being undefined, is incorrect. id has the expected value.
Therefore the error is that the API is giving an error in response to this call:
pool.query(
'DELETE FROM Players WHERE player_id = ?' ,
[5],
(error,results) => {
if (error) {
reject(error)
} else {
resolve(`Player deleted with ID: ${id}`);
}
}
)
So why don't you run exactly that, in simplified form like this:
pool.query(
'DELETE FROM Players WHERE player_id = ?' ,
[5],
(error,results) => {
console.log("Error:",JSON.stringify(error,null,2),"Results:",JSON.stringify(error,null,2))
}
)
This sends an explicit request to pool.query, and explicitly reveals the output. I don't know the format the pool.query API is expecting: perhaps it is not quite right?