2015-08-25 15 views
5

Sto cercando di passare attraverso il risultato di una query fino alla mia vista in Express. La query viene eseguita utilizzando mongodb che conta i punti totali degli utenti collettivi.Passa i dati alla vista in Express

Quando si tenta di passare attraverso il conteggio come variabile, ottengo

ReferenceError: /Sites/test/views/dashboard.ejs:76 

che si riferisce al <% = totalpoints%> secondo me ejs. Di seguito è riportato il mio codice in app.js

app.get('/dashboard', function(req, res) { 

    User.find({}, function(err, docs) { 
     console.log(docs); 
    }); 

    User.find({ 
     points: { 
      $exists: true 
     } 
    }, function(err, docs) { 
     var count = 0; 
     for (var i = 0; i < docs.length; i++) { 
      count += docs[i].points; 
     } 
     return count; 

     console.log('The total # of points is: ', count); 
    }); 

    var totalpoints = count; 

    res.render('dashboard', { 
     title: 'Dashboard', 
     user: req.user, 
     totalpoints: totalpoints 
    }); 

}); 

Qualche idea su come posso passare il risultato della query?

risposta

6

Il nodo esegue la query in modo asincrono. Cioè, il risultato della query non viene restituito immediatamente. È necessario attendere fino a quando il risultato non viene restituito e vengono utilizzati i callback per eseguire ciò. Pertanto, la chiamata della pagina di rendering deve avvenire all'interno della richiamata. Prova a modificare la tua funzione in questo modo.

app.get('/dashboard', function(req, res) { 

    User.find({}, function(err, docs) { 
     console.log(docs); 
    }); 

    User.find({ 
     points: { 
      $exists: true 
     } 
    }, function(err, docs) { 
     if(err){ 
      console.log(err); 
      //do error handling 
     } 
     //if no error, get the count and render it 
     var count = 0; 
     for (var i = 0; i < docs.length; i++) { 
      count += docs[i].points; 
     } 
     var totalpoints = count; 
     res.render('dashboard', { 
     title: 'Dashboard', 
     user: req.user, 
     totalpoints: totalpoints}); 
    }); 


}); 
+0

Dubito che 'attese res.render' coltivano per il ciclo è stato completato – Vishnu

+0

@ServerSideSkittles usare' User.aggregate' per trovare la somma dei punti invece di aggiungere in un ciclo for. http://mongoosejs.com/docs/api.html#aggregate-js – Vishnu

+4

@Mahesh 'i cicli for' sono sincroni, quindi sì,' res.render() 'aspetterà". – robertklep

1

Node.js è asincrono in natura, sarà eseguita res.render prima di ottenere i dati dal mangusta. Prova il seguente codice.

app.get('/dashboard', function(req, res) { 

    User.find({}, function(err, docs) { 
     console.log(docs); 
    }); 

    User.aggregate({ $group: { 
    _id: null, 
    count: { $sum: "$points" } 
    }}, function(err, docs) { 
     if(err){ 
      console.log(err); 
      //do error handling 
     } 
     else 
      res.render('dashboard', { 
      title: 'Dashboard', 
      user: req.user, 
      totalpoints: docs.count }); 
     } 
); 

}); 
Problemi correlati