'Unexpected end of input error which comes very first time using DiskDB

I am working on node js application and using Diskdb as database. It gives error on very first time as follows:

undefined:0

^
SyntaxError: Unexpected end of input

In documentation I found solution of this as "please make sure it contains a valid JSON array, otherwise diskDB will return an empty array." So can any one suggest me how can I check this file contains json array or not?

I am loading collection using

db.loadCollections(['users']);
var u = db.users.findOne();
console.log(u); // []

How can I check [] exists or not in file?

One more question is there how can I test record exists or not with multiple conditions in diskdb.

e.g. in users.json contains

[{"email":"[email protected]","password":"123","status":"1","_id":"e39a62a9c7b94d76892f9c3bdfa22715"}]

and my code using which i am checking is as follows:

 db.loadCollections(['users']);

 var preCheck = db.users.find({"email" : req.body.email,"password" : req.body.password});
    if(preCheck !== undefined){

        if( (preCheck.email == req.body.email) && (preCheck.password == req.body.password) ){ 
            //logged in

            req.session.email = req.body.email;
            req.session.userId = preCheck._id;
            req.flash('success','Successful Login');
            res.redirect('/option');
        }
        else{

            req.flash('error','Email Id or Password is Incorrect');
            res.redirect('/login');
        }
    }

but it does not allow me to login.

Thanks!



Solution 1:[1]

Hope it helps you.

var db = require('diskdb');
db = db.connect('data', ['users']);  // it connects and loads the collection

var preCheck = db.users.findOne({
    "email"    : req.body.email,
    "password" : req.body.password
});

if(preCheck){
   //logged in
   req.session.email = req.body.email;
   req.session.userId = preCheck._id;
   req.flash('success','Successful Login');
   res.redirect('/option');
} else{
   req.flash('error','Email Id or Password is Incorrect');
   res.redirect('/login');
}

Solution 2:[2]

As your DB will either empty or contains the following json data.

[{"email":"[email protected]","password":"123","status":"1","_id":"e39a62a9c7b94d76892f9c3bdfa22715"}]

findOne() only shows first row of record of your collection. You can either filter this or not, it depends upon your requirements.

Use the find() in your Code, which you are already using:

var preCheck = db.users.find({"email" : req.body.email,"password" : req.body.password});
if(preCheck.length !== 0){
  if( (preCheck[0].email == req.body.email) && (preCheck[0].password == req.body.password) ){ 
           
    req.session.email = req.body.email;
    req.session.userId = preCheck[0]._id;
    req.flash('success','Successful Login');
    res.redirect('/option');
  }
  else{
     req.flash('error','Email Id or Password is Incorrect');
     res.redirect('/login');
  }
}
else{
req.flash('error','Email Id or Password is Incorrect');
res.redirect('/login');
}

As data will be in array of json so use preCheck[0] there to get the data. Your query is working as AND condition. For more details, you can check its GitHub Link.

Note: I suggest to use sqllite3 as database. It's asynchronous (almost a must-have), it's the most actively maintained, and it has the most stars on GitHub. You can get the tutorial at this Link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Glorfindel