导出的功能不适用于socket.io

时间:2018-10-07 17:23:02

标签: javascript

我导出的调用端点的函数应该发出一些响应,直到我的前端没有工作,尽管当该函数位于socket.io函数中的index.js文件中时,该函数也可以工作。

我的导出函数未显示任何错误,尽管在我的前端未显示聊天,该函数未导出的地方也可以正常工作。

我已经控制了socket.id和数据,并且一切似乎都可以正常运行,但是也许我缺少关于socket.id如何发出信号的东西?

我的功能(已导出-在外部文件中):

exports.getRefundCalculationApiCall = function (itemId, refundCalcEndPoint, domain, orderId, customerName, details, response, socket) {
console.log('ngrokurl 2' + refundCalcEndPoint)
console.log('domain2' + domain)
console.log('orderId2' + orderId)
console.log('itemId2' + itemId)
httpRequest.post(
    `${refundCalcEndPoint}/${domain}/${orderId}`,
    {
        json:
            {
                "line_items": [
                    {
                        "line_item_id": itemId, "quantity": 1
                    }
                ]
            }
    },
    function (error, resp, body) {
        if (!error && resp.statusCode == 200) {
            console.log(body)

            amount1 = body.refund.refund_line_items[0].amount;
            // constructing message to front-end with the refund expense.
            responseToFrontEnd = `Your refund amount would be ${amount1},  Do you accept this and wish to initiate the returns process?`
            console.log( + responseToFrontEnd)
            response = responseToFrontEnd
            data = [details.chatuser, response]
            console.log('data' + data)
            io.of('/main').to(socket.id).emit('response', data);

            console.log('socket.id' + socket.id);
            console.log('are we hitting here in the postRequest get refund calc?');

        }
        else {
            console.log('error' + error)
        }
    }
)

}

在索引文件中调用我的函数-(文件的调用给出了在索引文件中调用该函数但没有发出响应时得到的确切响应):

shopifyApiRequest.getRefundCalculationApiCall(itemId, refundCalcEndPoint, domain, orderId, amount1, details, response, socket)

当我在如下所示的套接字函数中的同一文件中使用整个对象时,该函数运行良好:

io.of('/main').on('connection', function (socket) {

我不确定我在做什么错,因为那里没有任何错误,但是我的预期结果是我的消息实际上以与导出函数相同的方式发送到前端,而不是包含在我的index.js文件中。

0 个答案:

没有答案
相关问题