I’m trying to use a dataset hook on our Irish Whiskey Club inputs in order to avoid adding duplicates from a members inputs when they click on the submit button (that is, if they enter the same Whiskey more than once it doesn’t get saved in the dataset). The dataset (:Clubinput") has the “Whiskey” name in column 1, the “Date” in column 2 and “Comments” in column 3. The “Clubinput” dataset has the inputs from all the members, so I need to filter the dataset by “currentUser” before doing a query on “Whiskey” in the code. Then I need to check for a duplicate before inserting their new entry’s into the dataset.
My current code always returns the errors: save operation failed: TypeError: _wixData2.default.filter(…).eq(…) is not a function _wixData2.default.filter(…).eq(…) is not a function
Clearly, I have something incorrect!
//Here's my data.js code.
import wixData from 'wix-data';
import wixUsers from 'wix-users-backend';
export function searchForDuplicates(value) {
return (wixData.filter("Clubinput")
.eq("ID", wixUsers.currentUser))
(wixData.query("Clubinput")
.eq("Whiskey", value)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
})
)}
export function Clubinput_beforeInsert(item) {
return searchForDuplicates(item.Whiskey)
.then((res) => {
if(res > 0) {
return Promise.reject("Duplicate");
}
return item;
});
}
If you meant to use the system owner field, so it should be “_owner” (but maybe you meant to your own custom field).
You used “Whiskey” as the field key, but a field key can never start with an upper-case letter, so there must be a mistake. The same for the Owner field. It cannot start with a capital O.
@jonatandor35 Thanks JD! When I read about using query, it wasn’t clear to me in the description to use the field name or field key. Even looking at several examples that do this for just one condition it wasn’t clear to me. I’m getting ready to try it again. I still think I need to use the ”and” qualifier tho, because I need both items to match in the collection row before declaring a duplicate. I’ll let you know if (when) I get it working! I appreciate the help! Last time I did any real coding was in FORTRAN! Yup…I’m old! Learning new things is fun, but sometimes can be frustrating.
Oh, I see what you mean about “and” now! The ”and” is always implied with two “eq”!
@jonatandor35 One further question… In the call to “searchForDulicates”, I have “item.Whiskey”. This is supposedly coming from a drop down list on the page where the list comes from a different dataset to be written to “Clubinput”. Again, “Whiskey” is the field name not the field key. Should this be the field key in “Clubinput”?
@terrancehudson
J.D. already covered that in his previous reply to you about using the field key.
…You used “Whiskey” as the field key, but a field key can never start with an upper-case letter, so there must be a mistake. The same for the Owner field. It cannot start with a capital O.
Change it to the field key and it should all be good to go.