簡體   English   中英

參數顯示為未定義-導出的函數JavaScript

[英]Parameters showing as undefined - Exported function JavaScript

我導出的函數參數值返回為未定義,但是我的其他函數也以相同的方式工作。 希望您能提供幫助!

順便說一句-我知道並不是所有的參數都包括在內,但直到現在還沒有起作用!

我的函數在postRequests.js文件中不起作用:

exports.getRefundCalculationApiCall = function (itemId, ngrokUrl, domain, orderId) {
    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;

                amount1 = body.refund.refund_line_items[0].amount;
                // constructing message to front-end with the refund expense.
                response = `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?' + response)


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

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

這是我嘗試在index.js文件中調用的方法:

console.log('ngrokurl' + ngrokUrl)
console.log('domain' + domain)
console.log('orderId' + orderId)
console.log('itemId' + itemId)
shopifyApiRequest.getRefundCalculationApiCall((itemId, ngrokUrl, domain, orderId));

我的錯誤:

ngrokurl 2undefined
domain2undefined
orderId2undefined
itemId2594597937215
errorError: Invalid URI "undefined/undefined/undefined"

我期待一個標准的答復。 我有什么明顯的失蹤嗎?

在index.js文件中,您正在調用getRefundCalculationApiCall方法,並在參數周圍加上兩組括號:

shopifyApiRequest.getRefundCalculationApiCall((itemId, ngrokUrl, domain, orderId));

應該在參數周圍只用一組括號來寫:

shopifyApiRequest.getRefundCalculationApiCall(itemId, ngrokUrl, domain, orderId);

多余的括號將所有四個參數分組為一個,然后將其作為itemId參數傳遞。 您最終在console.log('itemId2' + itemId)語句中打印了orderId值。 其他三個參數將被忽略,因此未定義。 這是一個簡單的例子:

 function test(arg1, arg2, arg3, arg4) { console.log('arg1: ' + arg1); console.log('arg2: ' + arg2); console.log('arg3: ' + arg3); console.log('arg4: ' + arg4); } console.log('Individual arguments:\\n'); test('one', 'two', 'three', 'four'); console.log('Grouped arguments:\\n'); test(('one', 'two', 'three', 'four')); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM