2017-08-16 63 views
0

我使用OrientJs通过node.js与OrientDB进行通信。 我已经实现了一个API来插入一个新的用户到数据库,在检查他是否存在。OrienDb.RequestError:发现未知会话x

var dbServer = OrientDB({ 
    host: 'localhost', 
    port: 2424, 
    username: 'root', 
    password: 'password' 
}); 

// Connect to db 'test' 
var db = dbServer.use({ 
    name: 'mydbtest', 
    username: 'root', 
    password: 'my_root_password' 
}) 

// Set the server... 

app.post('/insertUser/', function (req, res) { 
    let username = req.body.username 
    let password = req.body.password 

    //Checks on username and password ... 

    let fetcher = require('../fetcher/fetcher') 
    fetcher.userExists(db, username, password).then(function (exists) { 
     console.log(exists) //exists = true/false 
     if (!exists) { 
      db.open().then(
       db.let('user', function (user) { 
        user.create('vertex', 'User') 
         .set({ 
          username: username, 
          password: password 
         }) 
       }).commit().return('$user').one().then(function (result) { 
        db.close() 
        if (!result.undefined) { res.status(200).send(true) } 
        else { res.status(200).send(false) } 
       }).catch(function (e) { // The error is caught here 
        db.close() 
        console.error(e); 
        res.status(500).send({ message: 'Unable to save new user1' }) 
       }) 
      ).catch(function (e) { 
       db.close() 
       res.status(500).send({ message: 'Unable to save new user' }) 
      }) 
     } 

     else res.status(200).send(false) 
    }) 
}) 

哪里fetcher.userExists(db, username, password)是,返回true,如果用户确实存在,否则为false的功能。 当调用从邮差THI API,我收到此错误消息:

{ OrientDB.RequestError 
at child.Operation.parseError (C:\Users\sdp\node_modules\orientjs\lib\transport\binary\protocol33\operation.js:896:13) 
at child.Operation.consume (C:\Users\sdp\node_modules\orientjs\lib\transport\binary\protocol33\operation.js:487:35) 
at Connection.process (C:\Users\sdp\node_modules\orientjs\lib\transport\binary\connection.js:410:17) 
at Connection.handleSocketData (C:\Users\sdp\node_modules\orientjs\lib\transport\binary\connection.js:301:20) 
at emitOne (events.js:115:13) 
at Socket.emit (events.js:210:7) 
at addChunk (_stream_readable.js:252:12) 
at readableAddChunk (_stream_readable.js:239:11) 
at Socket.Readable.push (_stream_readable.js:197:10) 
at TCP.onread (net.js:588:20) 
name: 'OrientDB.RequestError', 
message: 'Found unknown session 13', 
data: {}, 
previous: [], 
id: 1, 
type: 'com.orientechnologies.common.io.OIOException', 
hasMore: 0 } 

该消息是Found unknown session 13,但它由2每个I调用服务时间增加的数量。

如果我把代码中if(!exists){}声明出来fetcher.userExists(db, username, password).then(function (exists) {..then块的正常工作,但这样做,如果用户存在,我可以检查。我可以弄清楚是什么问题。有人能帮我吗?谢谢。

注:我使用OrientDB社区2.2.24

+0

嗨,是否有可能您的会话过期或临时断开? –

回答

0

的问题是在fetcher.userExists(db, username, password)的数据库连接,在该方法中我做了一个查询发现,如果用户存在(以类似的方式我这样做在我发布的代码中)。所以,我打开了与db.open()的连接,然后在返回结果之前,我只关闭它,执行db.close()。我没有正确关闭它。 db.close()后的代码应该是在then块,像这样:

//... 
if (!exists) { 
    db.open().then(
     db.let('user', function (user) { 
      user.create('vertex', 'User') 
       .set({ 
        username: username, 
        password: password 
       }) 
     }).commit().one().then(function (result) { 
      db.close().then(function(){  // <--- ADD then BLOCK HERE 
       if (!result.undefined) { res.status(200).send(true) } 
       else { res.status(200).send(false) } 
      }) 
     }) 
    }) 
} 

所以,在打开的fetcher.userExists(db, username, password)方面,它就像我试图打开一个新的连接和旧的关闭之前执行的查询后。在db.close()之后将代码放入then()避免这种情况。