Hi,
Been trying to upload JSON data manually in form upload as below and also trying to check for duplication whether the data is already available in the database or not. I have used, Before Insert Hooks for checking the same. Checked for permissions also its all in SiteMember. Please find below the Hooks coding as well. The problem is not getting updated in the database. Need Help
// -----Page Script
import wixData from ‘wix-data’;
export function importButton_onClick(event) {
const items = JSON.parse($w(“#textBox”).value);
const collection = $w(“#collectionInput”).value;
items.forEach( (item) => {
let toInsert = {
“_id”: item._id,
“_createdDate”: item._createdDate,
“_updatedDate”: item._updatedDate,
“_owner”: item._owner,
“title”: item.title,
“empId”: item.empId,
“at_email”: item.at_email,
“at_date”: item.at_date,
“at_in”: item.at_in,
“at_out”: item.at_out,
“at_actual”: item.at_actual,
“at_regfullday”: item.at_regfullday,
“at_regtime”: item.at_regtime
};
wixData.insert(collection, toInsert)
.then( (results) => {
let item1 = results;
console.log(Added item: ${JSON.stringify(results)}
);
} )
.catch( (err) => {
console.log(err);
} );
} );
$w(“#textBox”).value = “”;
}
//----------------------HOOK SCRIPT
export function LM_Attendance_beforeInsert(item, context) {
console.log("in before insert hook");
console.log("at_email is " + item.at_email);
console.log("empId is " + item.empId);
console.log("at_date is " + item.at_date);
let p = new Promise(function(resolve, reject)
{
console.log("in promise body");
let at_email_q = wixData.query("LM_Attendance").eq("at_email", item.at_email);
let empId_q = wixData.query("LM_Attendance").eq("empId", item.empId);
let at_date_q = wixData.query("LM_Attendance").eq("at_date", item.at_date);
let overall_q = at_email_q.or(empId_q).or(at_date_q);
overall_q.count().then(count =>
{
console.log("after overall_q returns");
if (count === 0)
{
console.log("count is zero, no dups found, resolving the promise to the original 'item' so the insert can proceed");
resolve(item);
}
else
{
console.log("count is more than zero, rejecting the promise with an error message");
reject("count is more than 0, dups found!");
}
});
});
return p;
}