How to store data in express upon callback

127 Views Asked by At

I am using node, express, and mongo, along with the spark node module, which allows a piece of hardware I'm using to send data to my server. What I want to do is save that data in my mongo database, but I'm not sure how as I'm new to MVC and MEAN.

This is what I have in my app.js file.

//Above this is the rest of my app.js file (settings up the routes/views)
spark.on('login', function() {
    console.log("logged in to spark api");
    //populateTable();

  //Get updates for global test event
  spark.onEvent('event', function(data) {
    console.log("Event: " + data);
    //I want to store this data
  });




});

//Login to the spark api
spark.login({accessToken: '<accesstoken>'}); //After you log in, the spark.on function above is called

So whenever I receive an event, the spark.onEvent code gets executed. But how do I save that data? I think I should call a POST request to one of my routes, but how do I do that from app.js? Should I even be putting this code in app.js or should it go somewhere else?

1

There are 1 best solutions below

1
On

Assuming you don't want it stored on the database (which would be a separate question), you just want to store it in a variable?

var storedData = $resource("Your datatype resource");
spark.on('login', function() {
    console.log("logged in to spark api");
    //populateTable();

  //Get updates for global test event
  spark.onEvent('event', function(data) {
    console.log("Event: " + data);
    //I want to store this data
    storedData = data;
  });