为什么picturesArray总是为空

为什么picturesArray总是为空

This question already has answers here:
How do I return the response from an asynchronous call?
                            
                                (38个答案)
                            
                    
                2年前关闭。
        

    

var request = require("request").defaults({ encoding: null });

var picturesArray = [];
request.get('http://www.vorohome.com//images/assets/159314_887955.png', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
        console.log(data)
        picturesArray[0] = data;
    }
});
console.log(picturesArray);

最佳答案

因为你在打电话

console.log(picturesArray);


在异步操作完成之前。

如果将此日志添加到请求回调中-您将获得正确的结果。

关于javascript - 为什么picturesArray总是为空? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51592990/

10-11 23:44