Hello! I’ve written a beforeSave trigger to make sure that when a user creates a new project, the title is unique. I first call a query to make an array of all projects titles, then try to make sure the input title doesn’t match any of them. The error is firing regardless if the name is unique or not. Any help would be appreciated! (Also any recommendations to make this more efficient are welcome as well)
Moralis.Cloud.beforeSave("Projects", async (request) => {
const query = new Moralis.Query("Projects");
const queryResults = await query.find({useMasterKey:true});
const results = [];
for (let i = 0; i < queryResults.length; ++i) {
results.push({
"title": queryResults[i].attributes.title,
});
}
// do any additional beforeSave logic here
},{
fields: {
title : {
options: title => {
let match = 0;
for (let i = 0; i < results.length; ++i) {
if(title == results[i].title){
match++
};
};
return match == 0;
},
error: 'You must choose a unique name for your project.'
}
}
});