我有一個Node.JS文件,用於輸出頁面加載分析測試結果。我已將結果存儲在文件results.json
中,其中JSON.stringify()
。從帶有鬍子的JSON呈現HTML
launchChromeAndRunLighthouse('https://www.microsoft.com/en-us/', flags, perfConfig).then(results => {
fs.appendFile('results.json', JSON.stringify(results), (err) => {
if(err){ throw err; }
console.log('Data was appended to file!');
var myObj = results.json; //problematic
var JSON_to_HTML = mustache.render('This test was generated at this time: {{generatedTime}}.', myObj); //problematic
});
});
現在我想顯示在瀏覽器中的結果,所以我想翻譯的JSON成HTML。我想用鬍子這一點,但這些行不是爲我工作:
var myObj = results.json;
var JSON_to_HTML = mustache.render('Test was generated at this time: {{generatedTime}}.', myObj);
我得到錯誤「的結果是沒有定義」,JSON文件不能被這樣的鬍子被讀取。我無法用原始JSON初始化「myObj」,因爲它大概有一百萬行(我需要稍後爲一大堆頁面運行測試,所以我現在不能硬編碼)。
我不知道如何將這個JSON文件轉換成HTML。有沒有人有任何想法?我是Node和Mustache的初學者,任何提示都非常感謝。
'launchChromeAndRunLighthouse'必須返回undefined。你可以通過渲染一些靜態數據console.log(mustache.render('{{foo}}',{foo:'bar'}))''應該記錄'bar'來驗證鬍子是否工作。 –
當我在一個新文件中嘗試console.log(mustache.render('{{foo}}',{foo:'bar'}))'時,我收到一條錯誤消息,指出鬍子沒有被定義。我安裝它,最初在全球範圍內,但只是在本地。還有什麼我不得不使用小鬍子?編輯:我可以打開JSON文件並查看測試結果,它不是未定義的... –