Why does the RethinkDB query keep returning null even when the data is present?

Having some trouble with rethinkDB while trying to retrieve data by username using the filter function. Despite the data being present, rethinkDB is returning null.

    //Define Your Api//Define Your Api
import express from 'express';
import r from 'rethinkdb';

const router = express.Router();

router.post('/users',(req,res)=>{

    let username = req.body.data.username;
    let password = req.body.data.password;
    console.log(username,password);

    r.connect({db:"image"}).then((conn) => {
        r.table("users").filter({username:"arfo"}).run(conn,function (err, data) {
            console.log(data) //null
        })
    })

});

export default router

Update:

Instead of getting the expected data, I am receiving a Cursor object. Should I manipulate this data in any way?

Cursor {
  _type: 3,
  _eachCb: [Function],
  _conn:
   TcpConnection {
     host: 'localhost',
     port: 28015,
     db: 'image',
     authKey: '',
     timeout: 20,
     ssl: false,
     outstandingCallbacks: {},
     nextToken: 2,
     open: true,
     closing: false,
     buffer: <Buffer >,
     _events: {},
     _eventsCount: NaN,
     _closePromise: null,
     rawSocket:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'localhost',
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        destroyed: false,
        _bytesDispatched: 325,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        user: 'admin',
        password: '',
        read: [Function],
        _consuming: true } },
  _token: 1,
  _opts: {},
  _root: { [Function] args: [ [Object], [Object] ], optargs: {} },
  _responses: [ { t: 2, r: [Object], n: [] } ],
  _responseIndex: 0,
  _outstandingRequests: 0,
  _iterations: 0,
  _endFlag: true,
  _contFlag: false,
  _closeAsap: false,
  _cont: null,
  _cbQueue: [],
  _closeCb: null,
  _closeCbPromise: null,
  next: [Function] }

Answer №1

According to the information provided in the RethinkDB documentation, it appears that the run function returns (err, data). An example from the documentation is as follows:

r.table('marvel').run(conn, function(err, cursor) {
    cursor.each(console.log);
})

If you modify your code to this:

r.table("users").filter({username:"arfo"}).run(conn,function (err, data) {
    console.log(data)
})

It should eliminate the null log that was previously appearing.

Although I'm not a RethinkDB expert, based on the documentation it seems like to retrieve data from the response, you can use toArray on the cursor:

r.table("test").run( conn, function(error, cursor) {
    cursor.toArray( function(error, results) {
        console.log(results) // results is an array of documents
    })
})

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

What are the steps for setting up node7 on a Mac operating system?

Currently facing an issue while trying to install node 7 or above on my Mac OS. I executed the following command: $brew install node Warning: node-7.9.0 is already installed, but not linked. Even though it indicates that node-7.9.0 is installed, when I c ...

end the node.js automated testing process

I'm currently using Jasmine and Zombie JS to create automated tests. I have integrated Drone.io for Continuous Integration, and the tests are executing successfully. However, there seems to be an issue where after passing the tests, the process does n ...

Automated algorithm inspecting a variety of hyperlinks

Recently, I've developed an innovative anti-invite feature for my bot. However, there seems to be a minor glitch where the bot fails to remove any links sent within the enabled guild when the command is triggered. This issue specifically occurs in ver ...

Run a function after the return statement

Currently, I am utilizing nodejs along with expressjs for my API development. I am seeking a method to execute a function post calling res.json(). For instance, the API delivers data to the client, and I want to record that action without delaying the re ...

Using NodeJS and Express together with Ajax techniques

I am currently developing a web application that utilizes Ajax to submit a file along with some form fields. One unique aspect of my form is that it allows for dynamic input, meaning users can add multiple rows with the same value. Additionally, the form i ...

Retrieve the updated value following an updateOne() operation

Is it feasible to retrieve all updated values or values with a difference after calling updateOne()? _.each(gameResult, function(gameResult){ bulk.find({"user" : gameResult.user, "section" : gameResult.section , "code" : gameResult.code}).upsert().up ...

Sending data and redirecting to a new URL by linking multiple responses together

As a beginner in javascript, I wanted to confirm if my code is appropriate. I am currently using JavaScript to handle a PUT request. My main goal is to return the updated Sequelize model as a response and then redirect the user back to the local /. Can I ...

Utilizing IISNode and/or nodemon for efficient node.js development on Windows platform

For my node.js application running on Windows, I currently utilize IISNode both locally during development and on production hosting. Would incorporating nodemon (or a comparable module that monitors file changes and restarts node.exe when necessary) pro ...

What is the best way to secure videos and other static files with authentication in a next.js web application?

My goal is to provide static content, specifically videos, exclusively to authorized visitors. I want to secure routes so that they are only accessible to authenticated users. However, the challenge arises when trying to display a video on a page located i ...

What is the process for creating a local repository for Node.js npm?

When it comes to the building process in node js, there are a few goals that need to be called: Begin by calling npm install, which creates a folder called node_modules and places all dependencies from package.json into it. [for UI development] Execute a ...

Using a module's internal function as a callback in the "request" operation

Within my primary code, I execute the following: var module = require('./module') module.FooA(module.FooB); The contents of module.js are as follows: var request = require('request'); //using npm "request" exports.FooB = function(d ...

We encountered an issue: `TypeError: expressJwt function is undefined`

As I work on developing middleware for user authorization within my application, I encounter an issue while attempting to determine if a route necessitates signing in. The relevant snippet of code is presented below: const { expressJwt } = require ...

Using Express to request data from Mongo database and only receiving the path

I've been troubleshooting a websocket function that interacts with MongoDB to fetch some data stored in the system using 'get'. const User = require('mongoose'); const express = require('express'); const cal = require(&a ...

Exploring MongoDB API Pagination

Picture a scenario where a customer has a list of items with a limit of 10. When they need the next set of 10 items, they send a request with a skip of 10 and a limit of 10. However, what if some new items were added to or removed from the collection sinc ...

Struggling to pass front end form data to the backend API in a React and Express.js application?

I am experiencing some issues with saving data from a React form. In the backend, I can only save the auto-generated ID from Mongoose. The API is not saving email and password in the database, as when I log the request body in the server it shows an empty ...

Sending data in a POST request using Express

I'm currently attempting to send a POST request for a new user to Intercom using the request module, but I'm facing difficulties in getting the format right. Interestingly, I can successfully execute GET requests with request and even post data u ...

What causes the tweets' IDs to be altered by axios when parsing the response from the Twitter search API?

I am currently utilizing axios to send a GET request to the Twitter search API in order to fetch recent tweets that utilize a specific hashtag. To begin with, I ran tests on the twitter search API via Postman and noticed that the id and id_str tweet statu ...

Can you provide guidance on accessing the response API from Twitter?

Currently, I am in the process of creating a toy project using React and Express.js, similar to a basic SNS website. Usually, the server responds to a client request by sending back JSON data containing all the necessary information for rendering articles ...

Debugger in VSCode is not stopping at the designated break-point when a postman request is made

I've been working on an express.js application and testing it using Postman. Debugging hasn't been necessary until now, but I recently tried setting it up and encountered an issue. Following this tutorial, the debugging setup isn't yielding ...

An issue occurred with the session being undefined in Node.js Express4

I'm encountering an issue where my session is undefined in new layers even after setting the value within an "if" statement. /*******************************************/ /**/ var express = require('express'), /**/ cookieParse ...