TypeError: Cannot read property '_id' of null

Hi,

Can anyone please help me out with this? - See screenshot below. Thanks you soo much, I appreciate your help.

Mr.,

These errors are a result of the dataset not being completely loaded yet, so any function referencing the dataset will return an error. Have a look at the second example of the documentation as it will show you what you need to do.

https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

Also, if you see the screenshot - it is not showing me any errors.

It would also be a good idea to mention that you are following this tutorial here.
https://support.wix.com/en/article/corvid-tutorial-using-the-paid-plans-api-to-customize-ordering-and-payment

All the Code
The code in its entirety looks like this:

import wixWindow from 'wix-window';
import wixPay from 'wix-pay';
import wixPaidPlans from 'wix-paid-plans';
import wixUsers from 'wix-users';

$w.onReady(function () {

const currentPlanObject = $w("#dynamicDataset").getCurrentItem();
const planId = currentPlanObject._id;
const planPrice = currentPlanObject.price;

$w('#buyNow').onClick((event) => {

let user = wixUsers.currentUser;
let isLoggedIn = user.loggedIn;
if (!isLoggedIn) {
wixUsers.promptLogin().then(() => {
processPlan(planId, planPrice);
})
} else {
processPlan(planId, planPrice);
}
});
});

function processPlan(myId, myPrice) {

if (myPrice > 0) {
wixPaidPlans.orderPlan(myId).then(orderObject => {
wixWindow.openLightbox("Confirm", orderObject)
.then((goForIt) => {
if (goForIt) {
wixPay.startPayment(orderObject.wixPayOrderId);
}
});
})
} else {
wixPaidPlans.orderPlan(myId).then(orderObject => {
wixWindow.openLightbox("Congrats", orderObject);
})
}
}

Process the Order
Now let’s look at the function we created, processPlan(), to get payment for the plan.
We chose to use the Wix Paid Plans orderPlan() function for processing because we wanted to control and customize the entire order/purchase flow. When orderPlan is called, and a plan is successfully purchased or ordered, an onPlanPurchased event is triggered.
This is where you can enter your own code to customize your own order/purchase flow.
In our example, we customized the flow by:

  • If the plan costs money, asking for user confirmation using the Confirm lightbox. If the user confirms, we call orderPlan() and then process payment with the Wix Pay startPayment function.

  • If the plan is free, using the Congrats lightbox to let the user know the plan order is successful.
    The order details are contained in the orderObject object.
    When copying this snippet, make sure to change the names of your lightboxes to match your own.

function processPlan(myId, myPrice) {

if (myPrice > 0) {
wixPaidPlans.orderPlan(myId).then(orderObject => {
wixWindow.openLightbox("Confirm", orderObject)
.then((goForIt) => {
if (goForIt) {
wixPay.startPayment(orderObject.wixPayOrderId);
}
});
})
} else {
wixPaidPlans.orderPlan(myId).then(orderObject => {
wixWindow.openLightbox("Congrats", orderObject);
})
}
}

Want to use the standard, “out of the box” Wix flow for letting visitors purchase a plan?

  • Use the the Wix Paid Plans purchasePlan() function in the code instead of the orderPlan() function.

  • No need to call wixPay. The purchasePlan() function does that for you.

https://www.wix.com/corvid/reference/wix-paid-plans.html#purchasePlan

Examples

Purchase a non-free plan on button click

import wixPaidPlans from 'wix-paid-plans';
import wixPay from 'wix-paid-plans';

let planId = // Get the plan ID for a non-free plan

export function myPurchaseButton_click(event) {
  wixPaidPlans.purchasePlan(planId)
    .then( (myPurchase) => {
        let myOrderId = myPurchase.orderId;
        let myWixPayOrderId = myPurchase.wixPayOrderId;
        let myWixPayStatus = myPurchase.wixPayStatus;
    } );
  }


/* myPurchase:
 * {
 *  "orderId": \"b3fb3a4d-c223-4040-8bc3-8dfdefb1bafc\",
 *  "wixPayOrderId": \"06af8fd6-6c82-4b51-bc08-0b556aad6f4f\",
 *  "wixPayStatus": \"Successful"
 * }