0

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.

Michael
  • 1,454
  • 3
  • 19
  • 45
  • Where is the `socket.emit` statement used - is it part if the `db` function? Also, how does the server respond after a request failure - does it send a socket.io event back to the client? – traktor Nov 19 '19 at 22:27
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Nov 20 '19 at 00:56
  • "*Here is the promise I'm calling client side `socket.emit('updateExistingItem', NewItem, (boolean) => {});`*" - I don't see anything related to promises in there, or how this would call the promise-returning `updateExistingItem` function. You will need to show us the server-side end of that socket connection code. – Bergi Nov 20 '19 at 00:58
  • @Bergi I just updated my question. I can't post the full files in here mainly because they are pretty large files, and these are the only pieces that we need in this instance, the rest of the stuff in those files are just more sockets that pertain to other calls. – Michael Nov 20 '19 at 14:09
  • 1
    @Michael Thanks! I don't know socketio in detail, what does `callback(true);` do? Probably you should call that from the `catch()` block as well instead of ignoring the error. – Bergi Nov 20 '19 at 14:24
  • @Bergi I don't really know a whole lot about socketio either, but in api calls inside the catch block you can usually do a res(send.error) to the client side. I guess I thought that you could essentially do something similar with sockets as well. – Michael Nov 20 '19 at 14:34
  • @Bergi honestly I wonder if the boolean value that is in the socket.emit is what get's that callback value. I might be able to just see what that value comes back as when it fails, and I might be able to just check if it's false within that call. – Michael Nov 20 '19 at 14:36
  • @Bergi I added a callback(false) inside the catch() method inside socket.on, and then console logged the boolean value from client side and I got a response back! – Michael Nov 20 '19 at 14:47
  • The socketio documentation should tell you what the `callback` is and whether you should call it in the error case as well – Bergi Nov 20 '19 at 14:48

1 Answers1

0

Ok so I was able to find a decent solution to this on my own.

In my socket within my sockets.js file I put a callback within the catch method like so

socket.on('updateExistingItem', async (ItemObject, callback) => {
        try {
            const results = await updateExistingItem(ItemObject);
            if (results.affectedRows === 1) {
                callback(true);
            }
            callback(false);
        }
        catch (error) {
          callback(false);
        }
    });

From there I decided to console log the boolean value that is found in the socket.emit on the client side, and was able to get this callback value in return.

Now it looks something like this

socket.emit('updateExistingItem', NewItem, (boolean) => {
   if(boolean){
    alert('Successful!');
   }else{
    alert('UnSuccessful!');
   }
});

I now get updates from the client side if this promise gets rejected or resolved.

Michael
  • 1,454
  • 3
  • 19
  • 45