0

I fetch data from Database in Kinvey using 2 javascript functions. I want the first function to be executed, and the next call to wait 2 seconds before executing. Everytime I use setTimeout I get: My error is :

TypeError: first._onTimeout is not a function
    at Timer.listOnTimeout (timers.js:92:15)

My code is this:

router.post('/datacol/getuserdata', (req, res) => {
  request(
    {
      url: kinveyurl + 'getPrivacyLevel',
      method: 'POST',
      json: {
        "user_id" : req.body.user,
        "sensors" : parseInt(req.body.sensor),
        "contexts": parseInt(req.body.context),
        "day_no"  : parseInt(req.body.day),
        "data_collectors": parseInt(req.user.type)
      },
      auth: {
        user: config.kinvey.appKey,
        pass: config.kinvey.masterSecret,
        sendImmediately: true
      }
    }, ( err, resp, bod) => {
    if(err) console.error(err)
    console.log("User " + req.body.user + " Summarization Level = " + bod.summarization_level)

    // This here should execute after 2 seconds!!!
    // I try to set the timeout here!!
    setTimeout(request(
      {
        url: kinveyurl + 'getAccelerometerData',
        method: 'POST',
        json: {
          "user_id" : req.body.user,
          "summarization_level": parseInt(bod.summarization_level),
          "day_no"  : parseInt(req.body.day)
        },
        auth: {
          user: config.kinvey.appKey,
          pass: config.kinvey.masterSecret,
          sendImmediately: true
        }
      }, ( err, resp, sensorData) => {
      if(err) console.error(err)
      console.log(sensorData)
      res.render('datacolaccelerometer', { user : req.user, sensordata: sensorData })
    }),3000);
  })
})

I am new to express and I do not know what to place where. In short, I would like to know where I should place the setTimeout so I can defer the execution of my second call to the javascript function by 2 seconds, thank you!

6
  • 1
    Wrap your request call in a function. setTimeout( function(){//your code here}, 3000 ); Commented Jun 28, 2016 at 16:22
  • can you show it using my code? Commented Jun 28, 2016 at 16:23
  • I am not very sure that's the real problem that's why not adding it as an answer. What I meant is, move your request(){//rest of your code}, inside the function(){//paste the request code here} call, as I mentioned in above comment. Commented Jun 28, 2016 at 16:25
  • I did that initially, did not work, any other suggestions? Commented Jun 28, 2016 at 16:28
  • 1
    What error did you get then? I think, you probably need to shift your code inside the function(){} first. Commented Jun 28, 2016 at 16:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.