0

I'm really struggling with the concept of a promise and how to access data returned by one. I am trying to access order information on a Thank You page in Wix. I am then trying to send that data to Facebook. You can see from the below which console.log's work and which do not. Suggestions please!

import wixLocation from 'wix-location';
import {postToFacebook} from 'backend/facebookModule.jsw';
import {hashData} from 'public/pages/masterPage.js';
import {getUserIP} from 'public/pages/masterPage.js';

$w.onReady(async function () {

    $w('#thankYouPage1').getOrder()
        .then((order) => {
            const orderObj = order;
            console.log('this works! '+ orderObj.number)
        })
        .catch((error) => {
            console.log(error);
        });


    console.log('this DOES NOT work '+ orderObj.number)


    const data = {
        "data": [
            {
                "event_name": "Purchase",
                "event_time": Math.floor(Date.now() / 1000),
                "action_source": "website",
                "event_source_url": wixLocation.url,
                "user_data": {
                    "em": (await hashData(orderObj.BillingInfo.email)),
                    "client_ip_address": (await getUserIP()),
                    "client_user_agent": navigator.userAgent
                },
                "custom_data": {
                    "currency": orderObj.currency,
                     "value": orderObj.totals.total
                }
            }
        ]
    };

    console.log(orderObj.totals.total);

    //console.log(await postToFacebook(data));
    await postToFacebook(data);


});

The error is of course on my second console.log:

workerLogger.js:103 ReferenceError: orderObj is not defined

SOLUTION - You need await:

import wixLocation from 'wix-location';
import {postToFacebook} from 'backend/facebookModule.jsw';
import {hashData} from 'public/pages/masterPage.js';
import {getUserIP} from 'public/pages/masterPage.js';

$w.onReady(async function () {
    try {
        var orderObj = await $w('#thankYouPage1').getOrder();
    } catch (error) {
        console.log(error);
    }

    const data = {
        "data": [
            {
                "event_name": "Purchase",
                "event_time": Math.floor(Date.now() / 1000),
                "action_source": "website",
                "event_source_url": wixLocation.url,
                "user_data": {
                    "em": (await hashData(await orderObj.billingInfo.email)),
                    "client_ip_address": (await getUserIP()),
                    "client_user_agent": navigator.userAgent
                },
                "custom_data": {
                    "currency": await orderObj.currency,
                     "value": await orderObj.totals.total
                }
            }
        ]
    };

    //console.log(orderObj.totals.total);

    //console.log(await postToFacebook(data));
    await postToFacebook(data);


});
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
James Wilson
  • 809
  • 3
  • 14
  • 25
  • Short answer is to do `const orderObj = await $w('#thankYouPage1').getOrder()`. Please please please read through the dupe, though. You've got to learn how async programming works as some point. Otherwise, you'll just have this same problem again tomorrow somewhere else in your code. – Adam Jenkins Oct 10 '21 at 10:44
  • Thanks Adam. I have tried that already: const orderObj = await $w('#thankYouPage1').getOrder() - what should be inside the then? return order;? The console.log return undefined: console.log(orderObj) – James Wilson Oct 10 '21 at 10:52
  • There is no `then` in the code snippet I posted. – Adam Jenkins Oct 10 '21 at 10:57
  • Got it working. I was missing off await in front of orderObj. Thanks! – James Wilson Oct 10 '21 at 11:05

0 Answers0