2014-04-13 106 views
1

以下是我如何在NPM中使用請求模塊的簡短示例。它返回undefined,我很確定我正確使用xml2js。我是否濫用請求模塊中的「表單」選項將值傳遞給Goodreads API?謝謝!爲什麼我的Goodreads API請求返回undefined?

var req = require('request'); 

var resp = req.get('https://www.goodreads.com/book/isbn_to_id', {form:{ 
    'key' : '*****************', 
    'isbn' : '9780060513030' 
}}); 

var parseString = require('xml2js').parseString; 

parseString(resp, function (err, result) { 
    console.dir(result); 
}); 

回答

1

它是基於回調的異步API,不是承諾或常規返回值。

var req = require('request'); 
var parseString = require('xml2js').parseString; 

req.get('https://www.goodreads.com/book/isbn_to_id', {form:{ 
    'key' : '*****************', 
    'isbn' : '9780060513030' 
}}, function (error, response, body) { 
    //error handling goes here! 
    parseString(body, function (err, result) { 
    //error handling goes here, too! 
    console.dir(result); 
    }); 
});