我正在嘗試更新量角器 nodejs 中 JSON 文件的鍵值對(duì)。無(wú)法將“鍵”參數(shù)傳遞到“等于”運(yùn)算符的左側(cè)以更新 JSON 文件中的值。請(qǐng)找到以下代碼并幫助我解決此問(wèn)題:調(diào)用方法:this.test = async function () {await writeToJSON(title, test-name-07282020-1234);}方法:async function writeToJSON(key, value) { const { readFile, writeFile } = require('fs').promises; const jsonFilePath = `${__dirname}/testdata.json`; let data = JSON.parse(await readFile(jsonFilePath)); data.LANG.TEST2.Default.key = value; // line 5 ..due to key is not read from // the parameter passed in the method. It is trying to look the field 'key' in //json file to update the value. writeFile(jsonFilePath, JSON.stringify(data, 2, 2)).then(() => { console.log('Finished writing to json file') })}測(cè)試數(shù)據(jù).json: { "LANG": { "TEST1": { "Default": { "username": "value1", "password": "value2" } }, "TEST2": { "Default": { "username": "value1", "password": "value2", "title": "test-name-05252020-4786" } } }它在第 5 行失敗我希望密鑰讀作“標(biāo)題”并為其分配值“test-name-07282020-1234”。請(qǐng)?jiān)谶@里幫忙!
如何將方法的參數(shù)傳遞給賦值運(yùn)算符的左側(cè)?
楊魅力
2022-12-09 19:18:58