【发布时间】: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 的响应中没有
pricesitem/key。这就是prices.prices未定义的原因。 -
你为什么要解析字符串文字?
-
我尝试将价格从 csgofast 转换为像 bitskins 一样的模型,但是是的 json 是不同的,我的错....谢谢大家,我会尝试另一个模型 :)
标签: javascript json node.js foreach