Question:
How to pass in custom data to the context object in a data hook setup?
Product:
[Wix Studio Editor]
What are you trying to achieve:
I have a drop down combo box that gives me a value. I need to use that value to retrieve a field from the database. and then I get an id that I would like to store as part of the item taht’s being inserted.
What have you already tried:
I have setup a data hook as follows
export async function CustomPhotos_beforeInsert(item, context) {
//const selectedComboValue = context.customData.selectedComboValue; // Access custom data from the context
console.log("THIS IS THE CONTEXT OBJECT ", context);
//item.memberId = context.customData.memberId;
item.orderNumber = 1500;
item.memberId = context.userId;
return item;
}
And my data hook is connected to a submit button on a page. As part of my submit button I have the following
$w("#submitBtn").onClick(async () => {
const customData = {
selectedComboValue: $w('#studentDropDown').value.split(",")[0],
memberId: userId
};
fetch('/account/_functions/CustomPhotos_beforeInsert', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(customData)
})
.then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
} )
.then( (json) => console.log(json.someKey) )
.catch(err => console.log(err));
} catch (error) {
console.error("Error inserting data:", error);
}
});
})
My problem is, how to retrieve the selectedComboValue
in my insert hook function.
Additional information:
[Include any other pertinent details or information that might be helpful for people to know when trying to answer your question.]