I’m trying to create an object where one of the properties is equal to a property from a related object.
Specifically, on an earlier page I assign a ‘triggermapcode’ to the User. When the user creates an order, I want the Order object to have a property ‘triggermaporders’ that is equal to that user’s ‘triggermapcode’.
Based on my (limited) understanding of Corvid & Javascript, I should be able to use a method on the triggermapcode property, but this functionality does not seem to be working on Wix.
I have two code blocks below, some generic method code from W3Schools explaining methods, which doesn’t work on Wix, and my code.
W3Code. Even just understanding why this doesn’t work would be helpful.
//Code from https://www.w3schools.com/js/js_object_methods.asp
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person) // does not return fullName in Wix. why?
My code:
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users
$w.onReady(function () {
});
export function onclicktestquery(event) {
let user = wixUsers.currentUser;
let userId = user.id;
var Order = {
orderId:"four", //I know this should be a UUID. Separate challenge.
userId: userId,
triggermaporders: function () {
return wixData.query("UserData")
.eq("userId", userId)
.limit(1)
.find()
.then( (results) => {
let details = results.items[0];
return details.triggermapcode;
});
}
//At this point console.log shows the Order object has 'orderId' and 'userId' but not 'triggermaporders'.
let options = {
"suppressAuth": true,
"suppressHooks": false
}
wixData.insert("Orders", Order)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errorMsg = err;
} );
}