I created a function in CoffeeScript that calls the Jira REST API, instead of returning the complete json object, I only want to return a single sprint id value.
But it always returns the Object??
This is the function:
jiraGetActiveSprintId = (res) ->
options =
rejectUnauthorized: false
returnMsg = -1
auth = 'Basic ' + new Buffer("user" + ':' + "passw").toString('base64');
robot.http("http://jira.rabobank.nl/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAMNAME+and+Sprint+not+in+closedSprints()", options)
.headers(Authorization: auth, Accept: 'application/json')
.get() (err, response, body) ->
if err
-2
if response.statusCode is 404
-3
if response.statusCode is 200
data = JSON.parse body
ab=data.sprints[0].id
return ab
This is how I call the function:
sprId = jiraGetActiveSprintId(res)
console.log('sprId:' + sprId)
But this is wat the console.log shows:
sprId:[object Object]
Why is it not returning the variable ab?
If I log the returning object I get:
sprId: ScopedClient {
options:
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'jira.bla.nl',
port: null,
hostname: 'jira.bla.nl',
hash: null,
query: { jql: 'project = TEAM and Sprint not in closedSprints()' },
pathname: '/rest/greenhopper/1.0/integration/teamcalendars/sprint/list',
path: '/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAM+and+Sprint+not+in+closedSprints()',
rejectUnauthorized: false,
headers:
{ 'User-Agent': 'Hubot/2.18.0',
Authorization: 'Basic ASBC123478WxCb29tMQ==',
Accept: 'application/json' },
encoding: 'utf-8' },
passthroughOptions: { rejectUnauthorized: false } }
Okay, I found a workaround, I understand now that because of the async nature of the call, you get back a promise. So I changed the code to call another method when the data (sprintId) is available instead. Not sure if this is the best solution, but at least it works.
jiraGetActiveSprintId = (res, functionToContinueWith) ->
options =
rejectUnauthorized: false
returnMsg = -1
auth = 'Basic ' + new Buffer('user' + ':' + 'password').toString('base64');
robot.http("http://jira.rabobank.nl/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAM+and+Sprint+not+in+closedSprints()", options)
.headers(Authorization: auth, Accept: 'application/json')
.get() (err, response, body) ->
if err
id=-2
console.log "STATUS=" + response.statusCode
if response.statusCode is 200
data = JSON.parse body
functionToContinueWith(res, data.sprints[0].id)
if response.statusCode is 404
id=-3
continueJiraGetActiveSprintIssues = (res, sprintId) ->
console.log('sprintId:', sprintId)
jiraGetActiveSprintIssues = (res, issueType) ->
jiraGetActiveSprintId(res, continueJiraGetActiveSprintIssues)