Methods on a property

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;
} );
}

Hi, the reason that console.log is not showing the value of triggermaporders value at that point is that the query has not completed in order to return that value. The following article explains this well: https://support.wix.com/en/article/corvid-working-with-promises

I’ve re-structured the code so that the assignment of the order object doesn’t occur until the query completes, after the .then() line.

export function onclicktestquery(event) {
let user = wixUsers.currentUser;
let userId = user.id;
wixData.query("UserData")
.eq("userId", userId)
.limit(1)
.find()
.then((results) => {
  let details = results.items[0];
  var Order = {
   "orderId": "four", //I know this should be a UUID. Separate challenge.
   "userId": userId,
   "triggermaporders": details.triggermapcode
   }
   let options = {
   "suppressAuth": true,
   "suppressHooks": false
   }
   wixData.insert("Orders", Order)
  .then((result) => {
   let item = result;
  })
  .catch((err) => {
    let errorMsg = err;
  });
});
}

If you find the idea of Promises a bit difficult to grasp, take a look at Promises, Promises for a simple explanation.

That did it! thanks very much. I’m new to both promises and scoping, and I think it was the scoping I was really struggling with.