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.
![](https://static.wixstatic.com/media/65f7a4_a3396c7a51cf429ab0959fa6c0de714d~mv2.png/v1/fill/w_980,h_613,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/65f7a4_a3396c7a51cf429ab0959fa6c0de714d~mv2.png)
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
Hi @TDB7942! The error message you received, "the value does not match the field type reference," indicates that the value you are trying to insert into the Programs field is not a valid reference to a record in the Orders collection. Use async/await properly: You mentioned that you used async/await to make sure that the record in Orders is created before the record in Stats can reference it. However, it seems that you are not using await properly. In your code, you are using await with wixData.insert(), but you are not waiting for it to finish before calling wixData.save(). You should use await with both wixData.insert() and wixData.save() to make sure that the data is saved before moving on to the next step. Use console.log() to debug: You mentioned that you used console.log() to check the value of the Programs field. You can also use console.log() to debug your code and see what values are being passed around. Good luck!