我正在使用 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