2016-07-03 11 views
0

Ich habe eine Funktion in CoffeeScript erstellt, die Jira REST-API aufruft, anstatt das vollständige JSON-Objekt zurückzugeben, ich möchte nur einen einzigen Sprint-ID-Wert zurückgeben.CoffeeScript, wie String zurückgegeben und nicht Objekt Objekt

Aber es gibt immer das Objekt zurück ??

Dies ist die Funktion:

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 

Dies ist, wie ich die Funktion aufrufen:

sprId = jiraGetActiveSprintId(res) 
console.log('sprId:' + sprId) 

Aber das ist wat die console.log zeigt:

sprId: [object Object ]

Warum gibt es die Variable ab nicht zurück?

Wenn ich das zurückgegebene Objekt log ich:

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, ich eine Abhilfe gefunden, ich verstehe, dass jetzt wegen der Asynchron Art des Anrufs, erhalten Sie ein Versprechen zurück. Also habe ich den Code geändert, um eine andere Methode aufzurufen, wenn die Daten (sprintId) verfügbar sind. Nicht sicher, ob dies die beste Lösung ist, aber zumindest funktioniert es.

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

Hmm, nein, die mir neu etwas ganz anderes – tazzer

Antwort

0

Ihre Funktion gibt keine id zurück. Es gibt ein Versprechensobjekt zurück.

einige wie diese versuchen:

sprId = jiraGetActiveSprintId(res) 
sprId.then (id) -> 
    console.log('sprId:' + id) 
+0

Versprechen Objekte ist es, las ich darüber etwas ifo-. Aber ich kämpfe immer noch darum, dass es funktioniert. Ich habe versucht, Ihren Code, Es gibt zurück: Fehler TypeError: sprId.then ist keine Funktion – tazzer

+0

Meine Schuld. ScopedClient gibt kein Versprechen zurück. Sie müssen Calback in Promise konvertieren. Schau dir diese Frage an: http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises –