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!
requestcall in a function.setTimeout( function(){//your code here}, 3000 );request(){//rest of your code}, inside thefunction(){//paste the request code here}call, as I mentioned in above comment.function(){}first.