Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我在JSON中收到此类错误Unexpected token o
错误细节:

[对象对象]

SyntaxError:JSON中位置1处的意外令牌o

function address_transaction(address,callback)
{
var api_key='11111111111111';
var type='na';
var url='some url';

var request = require('request');
request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var new1 =JSON.parse({"address":address,"response":response});
    callback(new1);
  }
  else {
    console.log("Error "+response.statusCode)
  }
})
}

最佳答案

您需要在nodejs中知道的重要一点是第一个参数是一个错误,因此当您收到错误时,需要像callback(err)这样处理。如果得到结果,则返回此结果,例如callback(null, result)

function address_transaction(address, callback) {
    var api_key = '11111111111111';
    var type = 'na';
    var url = 'some url';

    var request = require('request');
    request(url, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                var new1 = Object.assign({}, {
                        "address": address,
                        "response": response
                    };
                })
            callback(null, new1);
        } else {
            console.log("Error " + response.statusCode)
            callback(err)
        }
    })
}

09-20 14:24