简体   繁体   中英

nodejs Cannot read property 'forEach' of undefined

guys i get this error in my nodejs application but i not understand what is wrong and what is problem with 'forEach'. TypeError: Cannot read property 'forEach' of undefined,

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);

prices.prices is undefined. You can however try to store the reponse/data in prices.txt

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM