3

I am new to rethinkDB,I am trying to find data by username using filter function. But rethinkDB returns null eventhough data existed.

    //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

Updated

it returns me a bunch of data like this do i have to manipulate this data

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] }
Nane
  • 2,370
  • 6
  • 34
  • 74

1 Answers1

3

From the RethinkDB docs, it looks like run returns (err, data). For example (from the docs):

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

So if you update your code to:

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

Then it should remove the null log that you were seeing.

I'm no RethinkDB expert, but from the docs it looks like if you want to get the data from the response then you can call 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
    })
})
dan
  • 1,944
  • 1
  • 15
  • 22
  • Is there any problem with rethinkDB – Nane Nov 23 '16 at 20:41
  • I've updated my answer. I think you might need to call `toArray()` on the cursor. Take a look at the troubleshooting docs: https://www.rethinkdb.com/docs/troubleshooting/ – dan Nov 23 '16 at 20:45
  • 1
    Also, there's a good example under the 'Filter documents based on a condition' section here: https://www.rethinkdb.com/docs/guide/javascript/ – dan Nov 23 '16 at 20:49