如何在NodeJs框架Adonisjs中将异步数据从Controller传递到View [英] How to Pass async data from Controller to View in NodeJs framework Adonisjs

查看:152
本文介绍了如何在NodeJs框架Adonisjs中将异步数据从Controller传递到View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是nodejs的新手,正在与名为adonisjs的firebase和nodejs框架作斗争.我想问一下如何从控制器传递数据进行查看?这是异步的,因为您知道Firebase是异步的.

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

推荐答案

您可以使用AJAX这样操作:

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      
     }
})

这篇关于如何在NodeJs框架Adonisjs中将异步数据从Controller传递到View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆