Unable to retrieve record with MongoJs

I am experiencing an issue with my login system connected to a MongoDB database - the result is always 0 even when searching for a record I know exists.

thePass = passWord.value
theUser = userName.value

loginButton.onclick = function() {

    console.log(theUser)
    socket.emit('login', {username: theUser, password: thePass})

}

const mongojs = require("mongojs")
const db = mongojs('localhost:27017/shadowRiseDB', ['player'])

var correctDetails = function(data, cb) {
        db.player.find({username: data.username, password: data.password}, function(err, res) {
            console.log(res.length)
            if(res.length > 0)
                cb(true);
            else
                cb(false);
            });
        }

socket.on('login', function(data) {
        theDetails = data;
        correctDetails(theDetails, function(res){
           if(res) {
               socket.emit('loginDetails',{success:true});
           } else {
               socket.emit('loginDetails', {success:false});
           }
       })
    })

Despite my efforts, the if statement doesn't trigger as 'res' always returns 0 when trying to search for something in my database. What could be causing this issue?

Answer №1

Consider transforming the response into an array prior to verifying its length:

if(response.asArray().length>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

Register with your Facebook account - Angular 4 Typescript

After extensive searching, I have yet to find a clear answer to my question... This is my first time delving into the world of Social Networks. I am curious to know if there are options for Facebook and Twitter sign-ins using Typescript. It seems that Go ...

Ways to include additional details in each row of an autocomplete feature

I have successfully implemented autocomplete for venues worldwide, but now I want to display the address of each venue below its name. For example, if the autocomplete suggests the venue name as ABCD, I want the address of that venue to appear right benea ...

Automated browsing: identifying the difference between AJAX and iframes

During my automated requests (scheduled at specific times, without user involvement), I have noticed that xmlHttpRequest includes extra http headers. In order for the server not to be able to distinguish these requests as automated (they should appear exa ...

I am unable to save only one specific value, as the entire database is being saved instead

When I select a project from the drop-down list, it saves all the data from the selection. Please refer to the image below. function sample($con){ $select = "SELECT * FROM project_tbl"; $select_result = mysqli_query($con,$select); if (mysqli_n ...

Modify JSON property values using MongoDB query results in a NodeJS application

I have a JSON data structure as shown below: var outputJson = [ { 'Product' : 'TV', 'isSelected': 0 }, { 'Product' : 'Radio', 'isSelected': 0 }, ...

Breaking down a number using JavaScript

Similar Question: JavaScript Method for Separating Thousands I'm looking to find a way to separate numbers by a thousand using JavaScript. For example, I would like to turn "1243234" into "1 243 234", or "1000" into "1 000" and so on. (sorry for ...

Replicating a tag and inputting the field content

Scenario: An issue arises with copying a label text and input field as one unit, instead of just the text. Solution Needed: Develop a method to copy both the text and the input field simultaneously by selecting the entire line. Challenge Encountered: Pre ...

Failed to establish Modbus TCP connection

Currently, I am utilizing the "Panasonic FP7" master PLC along with their official software "FPWIN GR7" to monitor data flow on my PC. However, since the software lacks certain functions, I have decided to develop a solution using nodeJS. Below is the code ...

What is the best way to retrieve information utilizing Http.get within my project?

I have a TypeScript file containing user data: File path: quickstart-muster/app/mock/user.mock.ts import {User} from "../user"; export const USERS:User[]=[ new User(....); ]; I have a service set up at: quickstart-muster/app/services/user.service.ts ...

Encountering difficulty inserting ajax response into a specific div element

What could be the issue? I've included the getElementById and I am receiving a response, as confirmed by Firebug. The response is correct, but for some reason it's not populating my div area. <script> $(document).ready(function () { $( ...

Access the value of a textbox within a gridview using JavaScript within a div tag

I am working on a project where I have a textbox placed inside a div tag within a gridview. Here is the code snippet: <asp:TemplateField HeaderText="Color"> <ItemTemplate> <div id="preview" style="width:100%; ...

The efficient method of storing application activity logs using one schema and minimal details in MongoDB within a Node.js environment

Looking for a way to efficiently log application actions in a Node.js project with MongoDB database. We aim to include limited information such as success, action name, user id, time, etc. Any suggestions or best practices for achieving this in Node.js w ...

How can AngularJS focus on the last element using ng-repeat and md-focus?

, there is a code snippet that utilizes AngularJS ng-repeat directive to iterate through 'items' and 'md-autofocus' attribute to focus on the last element. The code is contained within an md-dialog with vertical scrolling functionality. ...

Error encountered in React caused by axios get request as data is undefined

Struggling to retrieve data from a MongoDB collection, the process goes smoothly in Insomnia, but I encounter an error when making a request with axios: Uncaught TypeError: Cannot read properties of undefined (reading 'games') Utilizing a custom ...

Changing the Div heights in Material UI with grid layout customization

I have a project that requires me to implement material-ui. Is there a way I can adjust the width and height of the div containing the "Sign In With" text (as shown in the first image) to bring the buttons closer to the text? Transformation from this: ht ...

Effectively accessing the Templater object within a user script for an Obsidian QuickAdd plugin

I have developed a user script to be used within a QuickAdd plugin Macro in Obsidian. The Macro consists of a single step where the user script, written in JavaScript following the CommonJS standard, is executed. Within this script, there is a task to gene ...

Development of Chrome Extensions, JavaScript dilemma

Hey there, I'm new to JavaScript and I've been diving into the world of creating Chrome extensions. I'm trying to set up a content script and browser action, but I'm struggling to get it up and running. I know I'm probably making a ...

A guide to displaying API response data in a React JS application

As a beginner in react JS, I've encountered a persistent issue. Here is the code: import React from 'react'; class SearchForm extends React.Component { async handleSubmit(event){ event.preventDefault(); try{ const url ='/jobs/ ...

Unit test produced an unforeseen outcome when executing the function with the setTimeout() method

When manually testing this code in the browser's console, it performs as expected. The correct number of points is displayed with a one-second delay in the console. const defer = (func, ms) => { return function() { setTimeout(() => func.ca ...

Schedule Master: A sophisticated tool for time management

I have been following the instructions to implement a date time picker from this tutorial. I downloaded the necessary js and css files and placed them in the respective directories. However, when I click on the calendar icon, the calendar does not pop up. ...