How to Pass async data from Controller to View in

2019-08-30 02:08发布

I'm new to nodejs and struggling with firebase and nodejs framework called adonisjs. I want to ask how I can pass data from controller to view? which is async because you know firebase is async.

const firebase = use('App/Controllers/Http/FirebaseController')

class CityController {
  index({view}){
    var db = new firebase().admin().firestore();
    var citiesRef = db.collection('city');
    var data = []
    var allCities = citiesRef.get()
          .then(snapshot => {
            snapshot.forEach(doc => {
              data.push({
                title : doc.id,
                data : doc.data()
              })
            });
            // send data from here?  how?
          })
    return view.render('cities', {data : data}); // data is being sent as empty due to async
  }
}

module.exports = CityController

1条回答
对你真心纯属浪费
2楼-- · 2019-08-30 03:02

You can do that using AJAX like this:

$ajax.({
 url:Your URl Code Here //http://0.0.0.0:333/api/.../+ ID +/...,
 type:"POST",
 status:....,
 compelete:function(result,status...){
    //do what you want in this function      
     }
})
查看更多
登录 后发表回答