【问题标题】:nodejs Cannot read property 'forEach' of undefinednodejs无法读取未定义的属性'forEach'
【发布时间】:2018-01-10 22:28:30
【问题描述】:

伙计们,我在我的 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);

【问题讨论】:

  • API 的响应中没有prices item/key。这就是 prices.prices 未定义的原因。
  • 你为什么要解析字符串文字?
  • 我尝试将价格从 csgofast 转换为像 bitskins 一样的模型,但是是的 json 是不同的,我的错....谢谢大家,我会尝试另一个模型 :)

标签: javascript json node.js foreach


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2021-12-23
    相关资源
    最近更新 更多