I'm trying to access a data from a function, I thought it was an array so i made the JSON.parse() in order to split the array objects into var objects, but it seems to not be a real array, so I don't know what it is.
This is the code:
app.post('/', function(request, response)
{
console.log('POST OK');
mongoose.connect('mongodb://localhost');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback()
{
database.findByName(request.body.username.toLowerCase(), function(error, data)
{
if(error)
{
response.render('index',
{
'Title': Title,
'result': 'Error, please try again later.'
});
}
if(data.length > 0)
{
console.log(JSON.stringify(data));
if(request.body.username == data.username && request.body.password == data.password)
{
response.render('index',
{
'Title': Title,
'result': 'Log in success.'
});
}
else
{
response.render('index',
{
'Title': Title,
'result': 'username/password invalid.'
});
}
}
response.render('index',
{
'Title': Title,
'result': 'Not found'
});
});
});
});
One of the if statements is not working because data.object() is undefined so I made the log of the data and I get :
[{"username":"maggns","password":"22542214","other data"}]
The JSON.parse is not working, it throws me an error. Any idea how to parse this data?