0

我正在使用 OrientJs 通过 node.js 与 OrientDB 进行通信。在检查他是否存在之后,我已经实现了一个 API 来将一个新用户插入到数据库中。

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

wherefetcher.userExists(db, username, password)是一个函数,如果用户确实存在则返回 true,否则返回 false。从 Postman 调用 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。

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

注意:我正在使用 OrientDB Community 2.2.24

4

1 回答 1

0

问题是 db 连接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),就像我试图打开一个新连接并在旧连接关闭之前执行查询一样。避免这种情况then()后放入代码。db.close()

于 2017-08-18T12:47:31.043 回答