nodejs无法读取属性' forEach'未定义的

时间:2018-01-10 22:28:31

标签: javascript json node.js foreach

伙计们,我在我的nodejs应用程序中遇到此错误,但我不明白错误是什么,以及' forEach'是什么问题。 TypeError:无法读取属性' forEach'未定义的,

var request = require('request');
var log4js = require('log4js');
var logger = log4js.getLogger();
var fs = require('fs');
var prices;
setInterval(function(){
    request('https://api.csgofast.com/price/all', function(error, response, body) {
        prices = JSON.parse(body);
        var newprice = JSON.parse('{"response":{"success":1,"current_time":1464567644,"items":{}}}');
prices.prices.forEach(function(item) {
                                newprice.response.items['migration_time_validation'] = JSON.stringify(database_params);
                                newprice.response.items[item.market_hash_name] = {
                                        "value": item.price

                                }
                        });
        fs.writeFileSync('C:/Program Files (x86)/Ampps/www/test/prices.txt', body);
        logger.trace('New prices loaded.');
    });
}, 3);

1 个答案:

答案 0 :(得分:0)

prices.prices未定义。但是,您可以尝试将响应/数据存储在prices.txt

Array.from(prices).forEach(function(item){...};