我有一个简单的问题让我很困惑。我尝试寻找解决的其他类似问题,但找不到这个确切的问题。我正在尝试将一个字符串推送到一个空字符串中,但我收到了一个错误,提示推送不是一个函数?
/Users/3x7r3m157/Development/Javascript/leaderboard.js:37
competitor.push(data[i])
^
TypeError: competitor.push is not a function Super confused.
有任何想法吗?这是我的代码(我知道还有其他错误,但目前正在解决这个奇怪的推送错误):
const args = require('yargs').argv;
const fs = require('fs');
const util = require('util');
const leaderboard = require('./db.json')
const addCompetitor = (name) => {
leaderboard[name] = { points: [], times: [] }
console.log(leaderboard)
return leaderboard
}
console.log(leaderboard)
const addCompetitorTimes = (data) => {
let parser = 0;
var competitor = '';
let times = '';
let seconds = '';
let minutes = '';
let timeInSeconds = 0;
for (let i = 0 ; i < data.length ; i ++) {
if (parser == 0) {
//Stack Overflow peeps, weird error right here:
competitor.push(data[i])
}
if (parser == 0 && i == '_') {
parser ++
}
if (parser == 1) {
times.push(data[i])
}
}
parser = 0
for (let i = 0 ; i < times.length ; i ++) {
if (parser == 0) {
minutes.push(times[i])
}
if (parser == 0 && i == ':') {
parser ++
}
if (parser == 1) {
seconds.push(times[i])
}
}
seconds = parseInt(seconds);
minutes = parseInt(minutes);
timesInSeconds = seconds + (minutes * 60)
// leaderboard[name].times.push(timeInSeconds)
return timesInSeconds
}
addCompetitorTimes(args.competitorTimes)
fs.readFile('./db.json', (err, leaderboard) => {
if (err) throw err;
var db = JSON.parse(leaderboard);
// console.log(db)
});