I'm using OrientJs to communicate with OrientDB through node.js. I've implemented an API to insert a new User into the db, after checking whether he does exist.
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)
})
})
Where fetcher.userExists(db, username, password)
is a function that return true if the user does exist, false otherwise.
When calling thi api from Postman, i receive this error message:
{ 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 }
The message is Found unknown session 13
, but it increases the number by 2 each time I call the service.
If I put the code in the if(!exists){}
statement out of the then
block of fetcher.userExists(db, username, password).then(function (exists) {..
it works fine, but doing so I can check if the user exists. I can figure out what's the problem. Can someone help me? Thanks.
Note: I'm using OrientDB Community 2.2.24
The issue was the db connection in the fetcher.userExists(db, username, password)
, In that method I made a query to find if user exists (in a similar way I do in the code I posted). So, I opened the connection with db.open()
and then, before returning the result, I closed it simply performing db.close()
. I didn't close it properly. The code after db.close()
should be in the then
block, like this:
//...
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) }
})
})
})
}
So, after opening the connection in fetcher.userExists(db, username, password)
it was like I tried to open a new connection and execute query before the old one was closed. Putting the code in then()
after db.close()
avoid this.