Verify whether a group of documents includes a minimum of one document

I am currently working on implementing a custom Login feature in Meteor (despite being aware of the default login package). When a user submits the Login Form, a Meteor Method call is triggered:

Template.Login.events({
"submit form": function(event, doc){
    event.preventDefault();
    var username = doc.find("#login-username").value;
    var password = doc.find("#login-password").value;
    Meteor.call("isAdmin",username,password, function(error, result){
        console.log("Response from Method Call: ");
        console.log(error);
        console.log(result);
    });
}
});

This is supported by a Meteor method which checks if the collection contains the specified entry:

Meteor.methods({
  isAdmin: function (username, password){
      return !!Admins.find({username: username, password: password});   
  }
});

The issue arises when trying to differentiate between returning true only if the collection includes an entry for the user, and false otherwise. Currently, it is returning true regardless.

Answer №1

When you use the

collection.find([selector], [options])
function, it returns a cursor. This allows you to then use
collection.find([selector], [options]).count()
, which will give you the number of documents that match the criteria in the find() query. You can then easily check if this count is greater than zero.

In your specific situation:

Meteor.methods({
  isAdmin: function (username, password){
      return Admins.find({username: username, password: password}).count() > 0; 
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Older browser compatibility for Flexbox flex-flow alternative

<template> <div> <h2>Kanal Listesi</h2> <div class="container"> <div v-for="(channel,index) in channels" :key="index"> <div v-if="channel.ChName"> <img :src="'h ...

What causes data to update in a Vue template but not in the JavaScript portion of the code?

The topic in the template section updates when its value in the parent component changes, however, the same value in the script part remains the initial value it received at search_params.set('param1', this.topic);. Everything else in the code is ...

Using Javascript to make a nested JSON api request that includes several query parameters

I am currently working on creating an API for my upcoming website, but I am facing difficulty in obtaining multiple query results using the same URL in an express app. Sample data: var data = [{ articles : [{ id : '0', url : ...

How come my Vue template files within other Vue files are not displaying properly?

I am currently working with three primary Vue files: index.vue.js: <template> <div class="tax-map-wrapper"> <h1>this is the page where the map renders</h1> <Map/> </div> </template> <script> ...

How to toggle between arrays using ng-repeat

Currently, I am managing 3 arrays and wish to toggle between them using ng-repeat: $scope.fooDataObj = { array1:[{name:'john', id:'1'},{name:'jerry', id:'2'}], array2[{name:'bill', id:'1'},{name: ...

Covering a doughnut shape with a twisting spiral

I want to achieve a hula hoop covered in tape effect using three.js. The 3D model should look similar to the image below. https://i.sstatic.net/lj9cR.jpg I have been able to create the hoop in 3D space using TorusGeometry and pan around, but I am strugg ...

Tips for implementing a for loop on a JSON file in Node.js

I am facing an issue where I am trying to loop through a JSON array of objects using a for loop. Strangely, it only works when I explicitly pass the index as a number (e.g. parseJSON.sites[0]). When I try to use a variable within the loop, which is preferr ...

Using JSON.stringify() for custom serialization of an object

Imagine there is an object called a with properties: const a = { foo: 123, bar: 'example' } Now, this object is a part of another object called b like this: const b = { a: a, anotherField: "example" } While working with TypeScript, th ...

The JSONP method, utilizing .getJSON, is resulting in an undefined response

I'm currently experimenting with a task from Learning jQuery 4th edition by Karl Swedburg that involves Ajax, particularly JSONP. Here's the code I am using: $(document).ready(function(){ var url='https://api.github.com/users/jquery/repos& ...

Guide to arranging an object in ascending order based on a key's value using JavaScript

Is there a way to sort the following values: 1, 99, 1, 50 in javascript? Given the input: var map = {test:1, test2:99, test3:1, test4: 50} The desired output is: {test2:99, test4:50, test3:1, test:1} This is the approach I have attempted: function sort ...

Ending a route forcibly in Node.js

I have a nodejs app where I utilize some sub functions within the route /restapi. One specific sub function looks like this: console.log("Before Oauth"); await checkTheUser(clipApiID + ':' + clipApiSecret).then(res2 => { cons ...

Tips for incorporating a dynamic parameter (ID) into an axios API request in the following scenario

Can you demonstrate how to utilize dynamic parameter ID with axios api? Take a look at this example: this.$axios.post('/api/roles/{role_id}/permissions/{permission_id}') ...

The URIError occurred while attempting to decode the parameter '/December%2015,%' within the express framework

After setting up a middleware using the express framework that handles a URI with a date as a parameter, I encountered a small issue. app.get("/:date",function(req,res){ var result; var myDate=req.params.date if(moment(myDate).isValid()) ...

Issue with Jquery modal not functioning properly on second attempt

Currently, I am working on developing an application using CodeIgniter. However, I have encountered a problem where the modal window does not open for the second time. Here is a more detailed explanation of the issue: The form (view) in question contains ...

Error encountered when attempting to upload image on Twitter: missing media parameter

According to the latest Twitter media upload API documentation, it is recommended to first utilize either POST multipart/form-data or base64 encoded files when interacting with . However, encountering an error with code 38 stating "media parameter is mi ...

How can I create an input field that comes with a preset value but can be updated by the user with a different name?

I am in need of a solution that will enable a user to update text on a webpage dynamically. I have been unable to find any information on how to achieve this. Is there anyone aware of a method to implement this feature? Perhaps a library or utilizing Vue ...

Combine ES6 JavaScript modules into a single .min.js file using NodeJS

Some time ago, I developed a small JS/CSS framework with custom elements. Now, I am looking to enhance my workflow (compiling, merging, minimizing, etc.) using npm and node.js, which is new territory for me. Each component (module) was created in its own ...

Creating a fresh observable with Angular 2's RxJS filtering capabilities

Looking to deepen my understanding of RxJS within my Angular 2 application that utilizes angular-redux/store. I am fairly new to RxJS version 5.1.1 and currently in the process of grasping its concepts. My store is up and running smoothly, allowing me to ...

Which is the better choice: utilizing object literals or constructor functions?

I'm feeling a bit puzzled about the best way to create an object in JavaScript. It appears that there are at least two methods: one involves using object literal notation, while the other utilizes constructor functions. Is there a specific advantage o ...

Challenges arise when integrating ng-model with Angular Chosen

I'm working with a table that lists users, each row ending with a button that triggers a popup form. Inside the popup, I'm using a multiple select feature with Angular Chosen to display each user's 'interests'. However, despite fet ...