I'm not super familiar with promises, but I have a few that I'm implementing within my code and would really like an alert to appear from the client side when the promise is rejected.
Here is the socket I'm calling client side
socket.emit('updateExistingItem', NewItem, (boolean) => {});
NewItem is an object that contains the fields that I want to send.
This calls a promise in my inventory.js file
export const updateExistingItem = (ItemObject) => {
return new Promise(async (resolve, reject) => {
try {
const updateExistingItem = `UPDATE console.inventory_items SET Description = '${(ItemObject.Description).replace(/\'/g, "")}', Location = '${(ItemObject.Location).replace(/\'/g, "")}', ModelNumber = '${(ItemObject.ModelNumber).replace(/\'/g, "")}'`
const response = await db(updateExistingItem, `Updating Item`);
resolve(response);
} catch (e) {
console.log("ERROR inventory.updateExistingItem: " + e);
reject(e);
}
});
};
If a user on the client side puts in information in the NewItem object that doesn't cooperate with the SQL call, I need a way to alert that user that the information wasn't saved/updated. As of right now it's a little misleading to the user, because if they put in something that get's rejected, it looks like it succeeds from their end.
If anyone has solutions that would be fantastic! Thanks.
EDIT
Here is the socket in my sockets.js file
socket.on('updateExistingItem', async (ItemObject, callback) => {
try {
const results = await updateExistingItem(ItemObject);
if (results.affectedRows === 1) {
callback(true);
}
callback(false);
}
catch (error) {}
});
SocketIO being used in my server.js file
var server = http.createServer(app);
const io = socketIO(server);
io.on('connection', (socket) => {
require('./middleware/sockets')(socket);
});
There are 4 different files where these are used so this is why I only put the snippets that actually pertain to this specific call.