我在CoffeeScript中创建了一个调用Jira REST API的函数,而不是返回完整的json对象,我只想返回一个sprint id值。
但它总是返回Object ??
这是功能:
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
这就是我调用函数的方式:
sprId = jiraGetActiveSprintId(res)
console.log('sprId:' + sprId)
但是这是控制台.log显示:
sprId:[object Object]
为什么不返回变量ab?
如果我记录返回的对象,我得到:
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 } }
好的,我找到了一个解决方法,我现在明白,由于调用的异步性质,你会收回一个承诺。所以当数据(sprintId)可用时,我更改了代码以调用另一种方法。不确定这是否是最好的解决方案,但至少它是有效的。
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)
答案 0 :(得分:0)
您的函数未返回id
。它返回一个promise对象。
尝试这样的事情:
sprId = jiraGetActiveSprintId(res)
sprId.then (id) ->
console.log('sprId:' + id)