2016-11-23 73 views
3

我是新來的rethinkDB,我試圖通過使用過濾器函數的用戶名來查找數據。 但是,即使有數據存在,rethinkDB也會返回null。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] } 

回答

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 
    }) 
}) 
+0

看得出來更新它不工作 – Nane

+0

rethinkDB有沒有問題 – Nane

+0

我已經更新了我的答案。我想你可能需要在遊標上調用'toArray()'。查看故障診斷文檔:https://www.rethinkdb.com/docs/troubleshooting/ – dan

相關問題