Get data on server side..

Hi,

I have successfully setup a TPL file (dynamic HTML page) that gets data from a lua file.  However, I need to get data from a Mongo object and pass this data to the HTML file. The docs suggest I can use this method:

local obj, err = users:get(“id1234”) however I am not sure how to add a listener to be able to save the data from the results, and pass it into my data table. 

Code is as follows:

local page = core.pages.new()

local drivers = core.data(“drivers”)

function getLat3 (evt )

  if evt.error then

    print(evt.error)

  else

    driverLat3 = evt.result.lattitude

  end

  return driverLat3

end

drivers:get({route=“someRoute”},getLat3)

local data = {

latitude = needDataFromMongoObject,

longitude =  needDataFromMongoObject,

}

local body = page.template("/site/index.tpl", data)

page.response(body)

any ideas?

Thanks!

Hi,

All server-side methods are synchronous (but non-blocking) so there is no need for a callback.

So your info should be in the return obj as noted in the docs.

--example obj, err = drivers:get({route="someRoute"},getLat3) local lat = obj.latitude

You should generally check for errors though and run an if/else for your page response (or log it):

--example obj, err = drivers:get({route="someRoute"},getLat3) if not obj then page.response(err) --or log core.log(err) else page.response(obj.latitude) end

Let me know if that works for you.

-dev

Great thanks - what if I need to get multiple items?  So for instance:

obj, err = drivers:get({loopRoute=“City”})

local lat1 = obj.lattitude

local lon1 = obj.longitude

obj1, err = drivers:get({loopRoute=“Beach”})

local lat2 = obj1.lattitude

local lon2 = obj1.longitude

obj2, err = drivers:get({loopRoute=“Park”})

local lat3 = obj2.lattitude

local lon3 = obj2.longitude

Can I do that?

Hi,

Yeah, that should work just fine.

-dev

Thanks great! Thanks. One more question - I am trying to get my map markers to move based on data from my MongoDB. The problem is that when the HTML page is loaded, I am not sure how to grab any new updated data (without having to refresh the page). I am sure this can be done with AJAX or even a network.request(), but not sure where to start. Any idea?

Hi,

I’ve added a section in the docs explaining the process for AJAX calls. Let me know if you have any other questions.

http://docs.coroniumcore.com/server/modules/pages/ajax/

-dev

Hi,

All server-side methods are synchronous (but non-blocking) so there is no need for a callback.

So your info should be in the return obj as noted in the docs.

--example obj, err = drivers:get({route="someRoute"},getLat3) local lat = obj.latitude

You should generally check for errors though and run an if/else for your page response (or log it):

--example obj, err = drivers:get({route="someRoute"},getLat3) if not obj then page.response(err) --or log core.log(err) else page.response(obj.latitude) end

Let me know if that works for you.

-dev

Great thanks - what if I need to get multiple items?  So for instance:

obj, err = drivers:get({loopRoute=“City”})

local lat1 = obj.lattitude

local lon1 = obj.longitude

obj1, err = drivers:get({loopRoute=“Beach”})

local lat2 = obj1.lattitude

local lon2 = obj1.longitude

obj2, err = drivers:get({loopRoute=“Park”})

local lat3 = obj2.lattitude

local lon3 = obj2.longitude

Can I do that?

Hi,

Yeah, that should work just fine.

-dev

Thanks great! Thanks. One more question - I am trying to get my map markers to move based on data from my MongoDB. The problem is that when the HTML page is loaded, I am not sure how to grab any new updated data (without having to refresh the page). I am sure this can be done with AJAX or even a network.request(), but not sure where to start. Any idea?

Hi,

I’ve added a section in the docs explaining the process for AJAX calls. Let me know if you have any other questions.

http://docs.coroniumcore.com/server/modules/pages/ajax/

-dev