Flic Home

    Community

    • Login
    • Search
    • Popular
    • Users

    GET and POST variable

    Flic Hub SDK
    http request
    2
    2
    747
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mikaps7
      mikaps7 last edited by

      When clicking the flic button, I want to have an HTTP makeRequest GET function, save some of the information in a global variable, and use this variable in an HTTP makeRequest POST function.

      I am able to create a variable and change it during the GET function. However, the information is not saved and when I use the POST function, it uses the value the variable had during the initialization phase.

      This is the code:
      var runNumber = -1 // my global variable

      buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
      var button = buttonManager.getButton(obj.bdaddr);
      var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";

      http.makeRequest({
      	url: urlGet,
      	method: "GET",
      	headers: {"Content-Type": "application/json"},
      }, function(err, res) {
      	console.log("request status: " + res.statusCode);
      	runNumber = 5 // changing the variable
      });
      	
      http.makeRequest({
      	url: urlPost,
      	method: "POST",
      	headers: {"Content-Type": "application/json"},
      	content: JSON.stringify({"Units":15, "Run Number":runNumber}),		
      }, function(err, res) {
      	console.log("request status: " + res.statusCode);
      });
      

      The code above is not working the way I expected it to work. The run number in the post function stays -1 although I change it to 5 in the get function.

      Please advise and thank you 🙂

      Emil 1 Reply Last reply Reply Quote 0
      • Emil
        Emil FlicTeam @mikaps7 last edited by

        @mikaps7 you execute the requests at the same time parallelly. If you want to execute the second request after the first has competed, you must initiate it in the callback of the first, or later.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post