第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

語法錯誤:/Users/3x7r3m157/Development/Javascript

語法錯誤:/Users/3x7r3m157/Development/Javascript

慕村225694 2021-11-18 20:29:07
在這個我似乎找不到的錯誤中,我正在讀取一個名為 db.json 的 .json 文件,更新 json 并嘗試寫入 .json 文件。雖然我正在解析和字符串化 json,但我收到此錯誤:語法錯誤:/Users/3x7r3m157/Development/Javascript/db.json:JSON 輸入意外結(jié)束我的代碼如下: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) => {  console.log(data)  //this method takes a string of 'competitorName_time'  //parses it into substrings 'name' & 'time'.  //Afterwards it iterates through time in minutes and seconds as a string  //in the format of '00:00', then parses the string into substrings  //of minutes and seconds delimited by the ':' character.  //Thereafter it converts the string to integeters and converts the time  //into time in seconds for easy comparison of times.  //Finally it assigns the time to the competitor in the leaderboard and  //writes to json file db.json  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 && data[i] == '_') {      parser ++      continue    }    if (parser == 0) {      //Stack Overflow peeps, weird error right hurrr:      competitor += data[i]    }    if (parser == 1) {      times += data[i]    }  }  parser = 0  for (let i = 0 ; i < times.length ; i ++) {    if (parser == 0 && times[i] == ':') {      parser ++      continue    }    if (parser == 0) {      minutes += times[i]    }    if (parser == 1) {      seconds += times[i]    }  }  seconds = parseInt(seconds);  minutes = parseInt(minutes);  let minutesInSeconds = minutes * 60  seconds = minutesInSeconds + seconds  leaderboard[competitor].times = seconds  console.log(leaderboard)  fs.writeFileSync('./db.json', JSON.stringify(leaderboard));  return leaderboard}addCompetitorTimes(args.competitorTimes)
查看完整描述

2 回答

?
BIG陽

TA貢獻1859條經(jīng)驗 獲得超6個贊

問題是“require”為您解析了 json,但您使用 json.parse 對其進行了冗余解析。它已經(jīng)是一個對象,這失敗了。只是不要重復(fù)解析。需要就夠了。


查看完整回答
反對 回復(fù) 2021-11-18
?
千巷貓影

TA貢獻1829條經(jīng)驗 獲得超7個贊

您正在編輯您仍在閱讀的文件。首先更改路徑的名稱,我建議在使用 JSON 或讀/寫文件時始終使用 try catch:


  try {

   fs.writeFileSync(require.resolve('./new-db.json')), JSON.stringify(leaderboard));    

  } catch (error) {

    console.log(error)

  }


查看完整回答
反對 回復(fù) 2021-11-18
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號