Rethinkdb查询总是返回null尽管存在数据?

时间:2016-11-23 20:26:07

标签: node.js express rethinkdb

我是rethinkDB的新手,我试图通过用户名使用过滤功能查找数据。 但是,只要存在数据,rethinkDB就会返回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

更新

它返回了我这样的一堆数据,我必须操纵这些数据

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] }

1 个答案:

答案 0 :(得分:3)

RethinkDB docs开始,run看起来像(err, data)。例如(来自文档):

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

因此,如果您将代码更新为:

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

然后它应该删除您看到的null日志。

我不是RethinkDB专家,但是如果您想从响应中获取数据,那么从文档看起来就可以在光标上调用toArray

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