Hello I am creating an API and fetching data from rethinkdb for that I am using following code:
r.connect({
host: 'localhost',
port: 28015,
db: 'test'
}, function (err, conn) {
if (err) throw err;
r.table('users').filter({email: user_data.name}).count().run(conn, (err, count) => {
conn.close();
if (err) {
return res.status(404).send({ success: false });
}
else if (count == 0) {
return res.status(401).send({ success: false });
}
return res.send({ success: true });
});
});
But I need to create multiple API's and for that I don't want to repeat the connection code.
I want to create a common function to return rethinkdb connection.
I used following code:
global.con = r.connect({
host: 'localhost',
port: 28015,
db: 'test'
});
and using con as connection object but it's giving error:
Unhandled rejection ReqlDriverError: First argument to run
must be an open connection.
Using this:
function findItem () {
r.connect({
host: 'localhost',
port: 28015,
db: 'test'
}, function (err, conn) {
if (err) throw err;
return conn;
});
}
global.c = findItem();
but still c is undefined