I have a form on my site where users can enter a number into the subNum field. When the user submits the form, I want to check if the value they've entered already exists as a value in any existing document. This is all done in javascript/meteor
All form data gets put into an object like this:
participantObject = {
subNum: parseInt(number)
}
This object then gets inserted as a document into the Participants collection
When the submit button is clicked, I need to check whether the number they've entered has already been entered (i.e. exists as the value for subNum in any document). If so, prevent form submit and show an error
I'm trying the check with something like this:
var existingSubs = Participants.find(
{subNum: {$eq: participantObject.subNum}}
).count();
console.log(existingSubs);
I am hoping that the above will find me all documents where subNum is equal to the value entered (participantObject.subNum), and console log the count of the number of matching documents
The problem is that in the console log I see Unrecognized operator: $eq
Am I using the equality operator incorrectly?
Is there a more efficient way to perform a check like this?
You can omit $eq operator:
var existingSubs = Participants.find(
{subNum: participantObject.subNum}
).count();
Related
(This question has been changed a bit since some of the answers were posted. That is why they might seem a bit off-topic and/or out of context)
Hello! So basically, I have this string that is entered by a user (a caption for an image, for example), and an array of links/words that I want to "block" the user from entering. (This would be to prevent from swearing, advertising etc.)
So I need some code that checks whether a certain value exists in an array.
This is my array:
var blockedUrls = ["https://google.com", "https://facebook.com"]
and this is the values I want to check
var userInput = "Hello! Check out my cool facebook profile at https://facebook.com";
(This would normally be set to a value fetched from an input of some sort, the static text is just to simplify)
So this is what I have tried:
let values = userInput.split(" ");
values.forEach((i, value) => {
// inArray is a made-up-function in order to better explain my intention
// The function I need here is a function that can check whether the value of the "value" variable exists in the "blockedUrls" array.
if(value.inArray(blockedUrls)) {
return alert(`You can't say that word! [${value}]`);
}
});
So a summary: How do I check if any of multiple values exists in an array?
You can check if a value is in an array by using indexOf
var value = document.getElementById('myFile').value;
if (unAllowedLinks.indexOf(value) != -1) {
// value is in array
}
-1 is returned when the value is not found in the array, else it returns the index of the value.
If you want to be able to change the number of values in unAllowedLinks you’d be better off using indexOf(), like so:
function updateImage() {
if (unAllowedLinks.indexOf(document.getElementById('myFile').value) > -1) {
alert("This link is reserved");
} else {
// Use the value
}
};
CONTEXT
I am trying to create a search functionality allowing users to fill in multiple fields, submit, and see a list of matching items from one collection. I do this using a form on the front end, which updates session variables on back-end, which are then passed as query to a mongodb collection.
HOW IT SHOULD WORK
If a user submits a venue size, then venues of that size are shown. If only a location is typed in, then venues within that location are shown. If both a size and a location are submitted, then venues that match both criteria are shown.
HOW IT ACTUALLY WORKS
If nothing is filled in, pressing search yields all items in the collection. Submitting both location and size yields venues that match both criteria. However, filling in only one field and leaving the other empty yields nothing in results. I'm wondering why this might be - it's almost as if the query is searching for a field that literally contains ''... but then why don't I see this behavior when leaving both fields empty? Help much appreciated!
CODE SNIPPET
//Search Form Helper
Template.managevenues.helpers({
venue: function () {
var venueNameVar = Session.get('venueNameVar');
var venueLocationVar = Session.get('venueLocationVar');
if(venueNameVar || venueLocationVar){
console.log(venueNameVar);
console.log(venueLocationVar);
return Venues.find({
venueName: venueNameVar,
'venueAddress.neighbourhood': venueLocationVar
});
} else {
return Venues.find({});
}
});
The answer lies in your query
Venues.find({
venueName: venueNameVar,
'venueAddress.neighbourhood': venueLocationVar
});
If you don't have one of your vars set it will look like this...
{
venueName: undefined,
'venueAddress.neighbourhood':'someVal'
}
So it would match any venue that doesn't have a name and is in some neighborhood.
A better approach would be to only set query criteria if there's a value to search...
var query = {};
if(Session.get('venueNameVar')) {
query.venueName = Session.get('venueNameVar');
}
if(Session.get('venueLocationVar') {
query.venueAddress = {
neighbourhood : Session.get('venueLocationVar');
}
}
return Venues.find(query);
I think this will work a bit better for you!
I was hoping this would work:
var title = document.getElementById('cat_title');
var active = document.getElementById('cat_active');
var category = {
title: title.value,
active: $(active).prop("checked");
}
When the form is submitted, and if the checkbox is checked, it inserts "true" into the active field. When it's not checked, it sends nothing but I'd like it to actually send "false".
Example of saved document if form is submitted WITHOUT checkbox checked:
{
_id: "lklajdlfjkasdf",
title: "Some Title"
}
Example of saved document if form is submitted WITH checkbox checked:
{
_id: ";klajsdfadsf"
title: "Some Title",
active: true <-- saved as boolean, good.
}
I'd like to keep the logic within the array. I understand another method to handle this... well there are several. I can perform some if/else and push to the array, but I feel I'm missing something simple with this array.
Thank you!
-------- Edit ---------
Details requested:
I'm inserting the record as a document in MongoDB. When the checkbox isn't checked, it inserts nothing into the active key/value pair. To answer various comments throughout, I need this as a boolean value, not a string. I'm trying some of the suggestions now and will update. Thank you all very much.... I've also updated my question above in more detail.
Even if that code carried the false value up to the database insertion, inserting a false value into the database will only make it a null field, which is the equivalent of setting it to nothing.
So the two options you have are to:
1) Check for a null field when retrieving the rows from the database
OR
2) Use the string value of "false" in the database and check for that instead
I have the following code which I got directly from a tutorial in ExtJs:
updateUser: function (button) {
var win = button.up('window'),
form = win.down('form'),
record = form.getRecord(),
values = form.getValues();
record.set(values);
win.close();
Now, I am working on some modifications to suit my needs, what exactly do the following 3 lines return:
form = win.down('form'),
record = form.getRecord(),
values = form.getValues();
According to the documentation, the down() method above returns an Ext.Container.AbstractContainer that should not even be used according to the documentation. Furthermore, it does not have a getRecord() or a getValues() method. Can anyone explain what is going on here and what kind of objects those 2 calls return?
When you call up it looks for the ancestor of selector passed. When you call down it returns the descendant of the selector passed. form.getRecord() returns the model instance of the form. and form.getValues() returns the actual values entered in the form.
Ex: Model has 3 fields id, name, email and some config when you call form.getRecord() it returns the model instance, basically skeleton.
Where as form.getValues() returns the values entered.
Ex: If the following values are entered in the form id=1, name=xxxxx, email=aaaaa#test.com
when you call form.getValues() it returns an object
{
id:1,
name:'xxxxx',
email:'aaaaa#test.com'
}
I hope this answers you question
I have a form, that has about 10 text entries (user, address, email etc;)
and about 50+ entries that are quantity entries (users select 2x of this item, 5x of this item).
Now I inherited this form from someone else (now its my duty to keep it up to date, when the customer requests it).
I don't want to re-write it all, but I would like to add validation for the quantity fields.
Now the quantity fields are all named different things (b1,c55,d,12)
Basically I would like to write a script (but don't know how to search for this, or accomplish this) JS side, that would ignore the tags I know (user, address, email etc;) but check if the others are numbers (either empty - not selected, or 1-99)
Apply a class to the elements (my code uses a class check) like so:
<input type="text" name="b1" class="check" />
and the following jQuery code, which will only check those elements with the check class.
$('#myformid').submit(function(){
$(':input.check').each(function(){
field = $(this);
valInt = parseInt(field.val()); // value as an integer
if (isNaN(valInt) || (valInt < 1 || valInt > 99)) // displays an error if the val is < 1 or > 99
alert('Error in the field ' + field.attr('name') + ': must be number from 1-99'); // change this to whatever you want the form to do if there's an error
});
});
Basically what this does is the following: when the form is submitted, it goes through every field you'd like it to (denoted :input.class to catch each field with the correct class) and checks if it's (a) a number, and (b) less than 1 or greater than 99. If it's a number and is outside the range, it alerts the user of an error. You can change the alert() notification to whatever error management you'd like your form to have.