My code is in event.js
It is triggered by wixStores_onOrderPaid()
When a product is ordered from the store two collections are populated with data from the sale. One is called Orders and the other Stats. Orders is populated first. The Stats collection has two reference fields. One (Programs) references the Orders Collection (Title). The other (Member) references the PrivateMembersData Collection (loginEmail). Everything works perfect except I get “the value does not match the field type reference” for the Programs field. The working references you see in the Programs field in the image below are because I opened the collection and manually made those connections to the Orders collection. The code delivers the ! and text with no connection to the reference field.
I assume this is happening because of 1 of 2 reasons.
-
The record in Orders has to be created before the record in Stats can reference it. To address this I used async/await and even added a setTimeout() to hopefully give the Orders collection a chance to populate before the Stats Collection trys to make a reference to it. Is there an onReady I am supposed to use?
-
The data for the Program field in Stats is concatenated with
${}
because it is the merging of several data inputs in the user’s registration process. I did a console.log and it appear to be coming through the same (as Text??) as the Members field data which has no problem making the reference connection. I think the problem lies here but I don’t know what the problem is (string vs. “id”) or how to fix it.
What am I doing wrong? Thank you to anyone who can help.
events.js Backend Code
import wixData from 'wix-data';
export async function wixStores_onOrderPaid(event) {
for (let i = 0; i < event.lineItems.length; i++) {
let toInsert = {
"productName": event.lineItems[i].name.slice(5),
"year": event.lineItems[i].name.slice(0, 4),
"playerlName": event.lineItems[i].customTextFields[0].value,
"playerfName": event.lineItems[i].customTextFields[1].value,
"playerFullName":
`${event.lineItems[i].customTextFields[1].value}
${event.lineItems[i].customTextFields[0].value}`,
"paymentStatus": event.lineItems[i].paymentStatus,
"parentfName": event.buyerInfo.firstName,
"parentlName": event.buyerInfo.lastName,
"orderDate": event._dateCreated,
"orderId": event._id,
"school": event.lineItems[i].options[0].selection,
"grade": event.lineItems[i].options[1].selection,
"member": event.buyerInfo.id,
"note": event.buyerNote,
"order": event.number,
"active": true,
"title": (`${event.lineItems[i].name}
${event.lineItems[i].customTextFields[1].value}
${event.lineItems[i].customTextFields[0].value}
${event._id}`),
//This field goes into Orders and is its Primary Field
}
await wixData.insert("Orders", toInsert)
.then
wixData.save("Orders");//My attempt to make sure the Collections
wixData.save("Stats") //are updated??? Doesn't change result
.then
console.log(`${event.lineItems[i].name}
${event.lineItems[i].customTextFields[1].value}
${event.lineItems[i].customTextFields[0].value}
${event._id}`);
setTimeout(() => { //My attempt to pause incase Orders needed a second
//before Stats could make a reference to its Primary
//Field
let programName = `${event.lineItems[i].name}
${event.lineItems[i].customTextFields[1].value}
${event.lineItems[i].customTextFields[0].value}
${event._id}`
let statsInsert = {
"productName": event.lineItems[i].name.slice(5),
"year": event.lineItems[i].name.slice(0, 4),
"player":
`${event.lineItems[i].customTextFields[1].value}
${event.lineItems[i].customTextFields[0].value}`,
"program": programName, // references "Title"
"member": event.buyerInfo.id,
}
wixData.insert("Stats", statsInsert)
}, 4000) //4000milliseconds = 4sec.
}
}
#referenceFields Events #error #collections #fields #onReady