0

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?

2 Answers 2

1

It's malformed JSON, you can't parse it (short of a hacky route of using regexps or something). Assuming you're in control of the data, you should look to see how you're generating that string before placing it into mongodb.

Sign up to request clarification or add additional context in comments.

3 Comments

the data is saved as array object. and that's how im retrieving it.
i just made a function that would make the mongoose connection and retrieve the data, the api says that object is an array. but its confusing why its not working
Then whatever api/process that is inserting the data is broken and needs to be fixed. You cannot have a single string like "other data" by itself in an object. You have to have both a key and a value in a JavaScript object.
0

It looks like data is an array so you should be using:

if(request.body.username == data[0].username && request.body.password == data[0].password) {
  // response
}

4 Comments

isnt there another way to get this data without pointing index?
data is an array. The reason it's returned as an array is because your database.findByName can return multiple objects that have the name request.body.username.toLowerCase(). If you want only 1 object then you should use a function that returns an object instead of an array, like mongoose's findOne.
the api says that mongoose find() returns an array, while findOne()returns 1 thing, i thought that find would be the right for getting the whole record.
find returns an array of documents, findOne returns a single document. A document is the 'whole record', an array is multiple records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.