从另一个文件发送Socket.IO消息

时间:2018-09-18 07:10:48

标签: javascript socket.io

我正在尝试导出一个函数,该函数涉及通过socket.io发送消息,该消息具有套接字ID和一些用户详细信息。

我收到未定义详细信息的错误。

我的功能:

exports.getRefundCalculationApiCall = function (itemId, ngrokUrl, domain, orderId, customerName, orderNum, itemName1, refundAmount1, shippingamount1, shippingtax1, shippingmaxrefund1, order_id1, amount1
, kind1, gateway1, currency1, parent_id1, maximum_refundable1, quantity1, line_item_id1, restock_type1, price1, subtotal1, total_tax1, discounted_price1, discounted_total_price1, total_cart_discount_amount1, details, response, socket) {
console.log('ngrokurl 2' + ngrokUrl)
console.log('domain2' + domain)
console.log('orderId2' + orderId)
console.log('itemId2' + itemId)
httpRequest.post(
    `${ngrokUrl}/${domain}/${orderId}`,
    {
        json:
            {
                "line_items": [
                    {
                        "line_item_id": itemId, "quantity": 1
                    }
                ]
            }
    },
    function (error, resp, body) {
        if (!error && resp.statusCode == 200) {
            console.log(body)
            console.log('refund line items transactions information' + JSON.stringify(body.refund.transactions[0]));
            console.log('refund line items +++ information THIS IS THE ONE' + JSON.stringify(body.refund.refund_line_items[0]));
            refundAmount1 = JSON.stringify(body.refund.refund_line_items[0].price);
            order_id1 = body.refund.transactions[0].order_id;
            kind1 = body.refund.transactions[0].kind;
            gateway1 = body.refund.transactions[0].gateway
            currency1 = body.refund.transactions[0].currency;
            parent_id1 = body.refund.transactions[0].parent_id;
            shippingamount1 = body.refund.shipping.amount;
            shippingtax1 = body.refund.shipping.tax;
            shippingmaxrefund1 = body.refund.shipping.maximum_refundable;
            quantity1 = body.refund.refund_line_items[0].quantity;
            line_item_id1 = body.refund.refund_line_items[0].line_item_id;
            restock_type1 = body.refund.refund_line_items[0].restock_type;
            price1 = body.refund.refund_line_items[0].price;
            subtotal1 = body.refund.refund_line_items[0].subtotal;
            total_tax1 = body.refund.refund_line_items[0].total_tax;
            discounted_price1 = body.refund.refund_line_items[0].discounted_price;
            discounted_total_price1 = body.refund.refund_line_items[0].discounted_total_price;
            total_cart_discount_amount1 = body.refund.refund_line_items[0].total_cart_discount_amount
            maximum_refundable1 = body.refund.transactions[0].maximum_refundable;
            amount1 = body.refund.refund_line_items[0].amount;
            // constructing message to front-end with the refund expense.
            responseToFrontEnd = `Your refund amount would be ${refundAmount1}, for the item: ${itemName1}. Do you accept this and wish to initiate the returns process?`
            console.log('RESPONSE from getrefundCalc - work to FE?' + responseToFrontEnd)

            response = responseToFrontEnd
            data = [details.chatuser, response]
            io.of('/main').to(socket.id).emit('response', data);

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

}

然后有一个我的index.js文件,它具有主要的套接字函数和详细信息,当我在index.js文件中调用导出函数时就起作用了。

 sessionClient
        .detectIntent(request)
        .then(responses => {
            console.log('Detected intent');
            result = responses[0].queryResult;
            response = result.fulfillmentText;
            responseHappened = true;

            /*checking to see if a response from dialogflow contains itemID which means it is the OrderByNum successful response from webhook
            with variables to be taken from it to be used throughout.
            */
            var responseOrderByNum = response.includes('itemId');
            if (responseOrderByNum) {
                var jsonObj = JSON.parse(response)
                console.log('jsonObj here' + JSON.stringify(jsonObj));

                // contains the values from the webhook response to be used.
                customerName = jsonObj.customerName;
                itemIdArray = jsonObj.itemId
                orderId = jsonObj.orderID
                totalPrice = jsonObj.totalPrice;
                orderNum = jsonObj.orderNum;
                item1Title = jsonObj.itemNames[0];
                itemName1 = jsonObj.itemNames[0];
                itemName2 = jsonObj.itemNames[1];
                itemName3 = jsonObj.itemNames[2];
                itemName4 = jsonObj.itemNames[3];
            }
            if (result.intent) {
                console.log(`  Intent: ${result.intent.displayName}`);
                console.log(`  Query: ${result.queryText}`);
                console.log(`  Text/Response: ${result.fulfillmentText}`);
            } else {
                console.log(`  No intent matched.`);
            }

            data = [details.chatuser, response]
            io.of('/main').to(socket.id).emit('response', data);
        })
        .catch(err => {
            console.error('ERROR:', err);
        });

我收到聊天用户未定义的错误消息,大概是响应吗?

0 个答案:

没有答案