Getting Response of a function defined in another

2019-09-14 15:02发布

I have a socket function defined as

var funcs = require('./funcs');

    socket.on(EVENT_ACCEPT_ORDER, function(data, callback)
    {                     
       data = JSON.parse(data);
       var bookingId  = data.bookingId;
       var userId     = data.userId;

       console.log("Accepting booking...." + bookingId);
       var query = "UPDATE bookings SET bookingStatus = " + BOOKING_STATUS_ACCEPTED + " WHERE id = " + bookingId + " AND bookingStatus = " + BOOKING_STATUS_IN_QUEUE;

    con.query(query, function(err, rows,fields)
    {
        if(err)
        {
               console.log("mysql query error");
        }
        else
        {
            if(rows.changedRows > 0)
            {
               var indexOfUser = usersList.indexOf(userId);
               if(indexOfUser > -1)
                {
                   userSockets[indexOfUser].emit(EVENT_USER_ORDER_ACCEPTED);
               }

               callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});
       }
      else
          callback({"message": "Success","error":true});                                            
    }
    });                               
    }); 


Funcs is defined as

    module.exports = {

    "getBooking": function (con, bookingId)
                 {  
                    var query = "SELECT * FROM bookings WHERE id = " + bookingId + " LIMIT 1";
                    con.query(query, function(err, rows,fields)
                    {
                        if(err) 
                        {
                            console.log("mysql query error");
                        }
                        else if (rows.length == 1)
                        {
                            var booking = rows[0];
                            var userId = rows[0]['userId'];
                            var query = "SELECT id, name, phone, imagePath FROM users WHERE id = " + userId + " LIMIT 1";
                            con.query(query, function(err, rows,fields)
                            {
                                if(err) 
                                {
                                    console.log("mysql query error");
                                }
                                else if (rows.length == 1)
                                {
                                    booking['user'] = rows[0];
                                    return booking;
                                }
                            });
                        }
                    });
                }
    }

Everything is running fine except

callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});

in this function instead of booking, i am only getting

{"error":false,"message":"Success"}

Why am i not getting the booking function result?

1条回答
唯我独甜
2楼-- · 2019-09-14 15:50

You are not getting the result, because the result of the callback function in con.query is not returned to the caller of getBooking. It is the asynchronous pattern, which you are not processing correctly.

The way it is supposed to work is that the getBooking gets an extra argument: a function to be called when data are available (in an internal asynchronous call to con.query). Such a function is then provided by the caller and in this function you do whatever you want with the data:

funcs.js

"getBooking": function (con, bookingId, callback) {
    ...
    con.query(query, function(err, rows,fields) {
        ...
        // instead of return booking do
        callback(err, booking);
        ...
    }
}

main module

// instead of 
callback({"message": "Success","error":false, "booking": funcs.getBooking(con, bookingId)});
// do
funcs.getBooking(con, bookingId, function(err, booking) {
    callback({"message": "Success","error":false, "booking": booking});
});

I am afraid this is not the only issue in your code, but this should be the first to fix. Read further about processing asynchronous calls in general and specifically in node.js and fix other places in your code correspondingly.

查看更多
登录 后发表回答