SOLVED: I made a mistake and used the wrong file extension .jsw instead of .js
The code works perfectly and is tested if someone needs this.
Hi there,
I created a new file, events.jsw, in the Wix backend and stitched together some code in order to create a new database entry in case someone purchases a plan. I tried it with a new account but there was no database entry and I am having a hard time debugging the code because I don’t know where to read the console for the backend code? Then I could add console.log() statements and probably debug it myself.
This is the code from events.jsw.
import wixData from 'wix-data';
export function wixPaidPlans_onPlanPurchased(event) { // If an purchase event is fired
wixData.query("planEvents") // query the existing database and search for the userID
.eq("userID", event.order.userID)
.find()
.then( (results) => {
if(results.items.length > 0) { // if there is a database object with the userID inside
let databaseRowID = results.items[0]._id; // then get the database ID of the object
let updatedMaxUserLevel = results.items[0].maxUserLevel + event.order.validFor.period.amount;
let toUpdate = { // create a new object with the new data from the event
"_id": databaseRowID,
"userID": event.order.memberId,
"maxUserLevel": updatedMaxUserLevel,
"currentPlanValidFrom": event.order.validFrom,
"currentPlanValidFor": event.order.validFor.period.amount,
"currentPlanObject": event
};
updateDatabase(toUpdate) // update the current object with the new data
} else { // handle case where no matching items found.
let orderData = { // create a new database object from the event.data
"userID": event.order.memberId,
"maxUserLevel" : event.order.validFor.period.amount,
"currentPlanValidFrom": event.order.validFrom,
"currentPlanValidFor": event.order.validFor.period.amount,
"currentPlanObject": event
};
insertDatabase(orderData) // create a new database entry
}
})
}
function updateDatabase(object) {
wixData.update("planEvents", object)
.then ( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
}
function insertDatabase(object) {
wixData.insert("planEvents", object)
.then ( (results) => {
let item = results;
})
.catch ( (err) => {
let errorMsg = err;
});
}
I also have a database with the following structure:
Any help is appreciated. Thanks!