-1
私はこれを動作させようとしていますが、なぜそれがうまくいかないのかわかりません。nodejsのapiからJSONデータを解析できません
//require the poloniex library
var plnx;
var currenciesJSON;
if (plnx = require('plnx')){
console.log("Poloniex library intialized.");
}
//
a = 0;
setInterval(function() { intervalFunction() }, 1000);
function intervalFunction(){
plnx.returnCurrencies({}, function(err, data) {
currenciesJSON = data;
console.log(err);
console.log(currenciesJSON);
});
var currenciesOBJ = JSON.parse(currenciesJSON);
a++;
console.log("Check #"+a+" complete.");
}
私は次のエラーを取得しています:
[nodemon] starting `node main.js`
Poloniex library intialized.
undefined:1
undefined
^
SyntaxError: Unexpected token u
at Object.parse (native)
at intervalFunction (E:\CODING\nodejs\main.js:22:28)
at null.<anonymous> (E:\CODING\nodejs\main.js:10:26)
at wrapper [as _onTimeout] (timers.js:265:14)
at Timer.listOnTimeout (timers.js:110:15)
[nodemon] app crashed - waiting for file changes before starting...
あなたが受け取ったJsonは有効ですか?結果console.log(currenciesJSON)を取得し、www.jsonlint.comに入れて –
を検証します。あなたの '' currenciesJSON''に無効なJSON文字列が含まれているようです。 – mdziekon
currenciesJSONは、その値が非同期コールバック内で設定されているため、未定義です。あなたの 'currenciesObj = JSON.parse(currenciesJSON); a ++; console.log(...)'をコールバックの中に入れてください。 – ant