0

How can I select in javascript ? I use parse.com as a database, this is my code, but it doesn't work

app.get('/students', function (req, res) {
    var students = Parse.Object.extend("students");
    var query = new Parse.Query(students);
    query.find({
        success: function (results) {
            for (var i = 0; i < results.length; i++) {
                var students = results[i];
                alert(students.get('Name')+ ' - ' + students.get('Lname')+ ' - ' + students.get('class_id'));
                    res.render('students/index', { students: students});
            }
        }
        })

And the logs error:

I2014-07-18T15:47:16.779Z] Error: cannot call http.ServerResponse.end() multiple times
at end (http.js:428:13)
at renderErrorResponse (connect_proto.js:155:15)
at next (connect_proto.js:187:9)
at fn (express_response.js:758:25)
at View.exports.renderFile [as engine] (jade.js:1149:5)
at View.render (express_view.js:77:8)
at Function.app.render (express_application.js:516:10)
at res.render (express_response.js:763:7)
at query.find.success (routes/students.js:28:29)
at Parse.js:2:5786

Could you help me please ?

2 Answers 2

2

As per the error message, you're calling something multiple times that you should only call once.

You shouldn't call res.render(..) inside a loop. The idea is to gather the data needed (inside a loop if needed) and then call the render() method at the end.

You are looping through the results (which is a collection of students rows), though your naming is confusing as you are using "students" to refer to a single student... anyway, from what I can understand of your intent you could simply do the following:

query.find({
    success: function (students) {
        res.render('students/index', students);
    }
});

NOTE: I named the results parameter fed to the success handler students instead, since it is an array of students.

0
0

Thank's, and I find how to select in parse.com with javascript

app.get('/students', function (req, res) {
    var students = Parse.Object.extend("students");
    var query = new Parse.Query(students);
    query.find({
        success: function (results) {
            //var students_data
            for (var i = 0; i < results.length; i++) {
                var object = results[i];
                console.log(object.id + ' - ' + object.get('Nom'));
            }
            res.render('students/index', { students: results });

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.