"data" 参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。接收到对象实例
The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object
一旦有人在填写文本输入并回答 html 中的所有问题后单击“创建口袋妖怪数据”按钮,我想使用 json 创建口袋妖怪数据的保存文件
但出于某种原因,我收到此错误
(node:30348) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray,
or DataView. Received an instance of Object
at Object.writeFile (node:fs:2106:5)
at E:\Pogramming folder\Pokehunt Streamers Tool\app\pokehunt\src\index.js:48:10
这是我的代码
这是 renderer.js
const fs = require('fs');
const {ipcRenderer} = require('electron');
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
let hunt =
{
"name": "",
"imageurl": "",
"method": "",
"timer": "00:00:00",
"counter" : 0,
"shinycharm": false
};
document.getElementById("startBtn").addEventListener("click",()=>{
console.log("click!");
const pokename = document.getElementById("pokemon").value;
if(pokename!=null){
hunt.name = pokename;
hunt.imageurl = 'https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif';
const selectMethod = document.getElementById("Method");
hunt.method = selectMethod.value;
const shinycharm = document.getElementById("shiny");
switch(shinycharm){
case "no":
hunt.shinycharm = false;
break;
case "yes":
hunt.shinycharm = true;
break;
}
//for testing I did this log stuff
console.log(JSON.stringify(hunt));
const huntfinal = JSON.stringify(hunt);
ipcRenderer.send("create-document-trigger",huntfinal);
} else{
hunt.name = "shinx";
}
});
这是我的main.js
const { app, BrowserWindow, ipcMain, nativeTheme, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
let mainWindow;
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 955,
height: 975,
autoHideMenuBar: true,
webPreferences:{
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false,
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
//dark mode
ipcMain.handle('dark-mode:toggle', () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = 'light'
} else {
nativeTheme.themeSource = 'dark'
}
return nativeTheme.shouldUseDarkColors
})
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system'
})
ipcMain.on("create-document-trigger",(hunt)=>{
dialog.showSaveDialog(mainWindow,{
filters: [{name:"json files",extensions:["json"]}]
}).then(({filePath}) =>{
console.log("filepath: "+filePath);
fs.writeFile(filePath,hunt,(err)=>{
if(err){
console.log(err.message);
}
})
})
})
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
console.log 给我 {"name":"shinx","imageurl":"https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif","method":"pokeradar","timer":"00h:00m:00s","counter":0,"shinycharm":false}
这几乎是(gif URL 没有放宠物小精灵的名字,但这是我稍后会解决的另一个问题)正是我在 json 文件,但代码无法创建此 json 文件。
我能够解决这个问题。
我把ipcMain.on("create-document-trigger",(hunt)=>{
换成了ipcMain.on("create-document-trigger",(event,data) =>{
现在代码看起来像这样并且可以工作
ipcMain.on("create-document-trigger",(event,data) =>{
dialog.showSaveDialog(mainWindow,{
filters: [{name:"json files",extensions:["json"]}]
}).then(({filePath}) =>{
console.log("filepath: "+filePath);
fs.writeFile(filePath,data,(err)=>{
if(err){
console.log(err.message);
}
})
})
})
一旦有人在填写文本输入并回答 html 中的所有问题后单击“创建口袋妖怪数据”按钮,我想使用 json 创建口袋妖怪数据的保存文件 但出于某种原因,我收到此错误
(node:30348) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray,
or DataView. Received an instance of Object
at Object.writeFile (node:fs:2106:5)
at E:\Pogramming folder\Pokehunt Streamers Tool\app\pokehunt\src\index.js:48:10
这是我的代码 这是 renderer.js
const fs = require('fs');
const {ipcRenderer} = require('electron');
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
let hunt =
{
"name": "",
"imageurl": "",
"method": "",
"timer": "00:00:00",
"counter" : 0,
"shinycharm": false
};
document.getElementById("startBtn").addEventListener("click",()=>{
console.log("click!");
const pokename = document.getElementById("pokemon").value;
if(pokename!=null){
hunt.name = pokename;
hunt.imageurl = 'https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif';
const selectMethod = document.getElementById("Method");
hunt.method = selectMethod.value;
const shinycharm = document.getElementById("shiny");
switch(shinycharm){
case "no":
hunt.shinycharm = false;
break;
case "yes":
hunt.shinycharm = true;
break;
}
//for testing I did this log stuff
console.log(JSON.stringify(hunt));
const huntfinal = JSON.stringify(hunt);
ipcRenderer.send("create-document-trigger",huntfinal);
} else{
hunt.name = "shinx";
}
});
这是我的main.js
const { app, BrowserWindow, ipcMain, nativeTheme, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
let mainWindow;
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 955,
height: 975,
autoHideMenuBar: true,
webPreferences:{
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false,
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
//dark mode
ipcMain.handle('dark-mode:toggle', () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = 'light'
} else {
nativeTheme.themeSource = 'dark'
}
return nativeTheme.shouldUseDarkColors
})
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system'
})
ipcMain.on("create-document-trigger",(hunt)=>{
dialog.showSaveDialog(mainWindow,{
filters: [{name:"json files",extensions:["json"]}]
}).then(({filePath}) =>{
console.log("filepath: "+filePath);
fs.writeFile(filePath,hunt,(err)=>{
if(err){
console.log(err.message);
}
})
})
})
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
console.log 给我 {"name":"shinx","imageurl":"https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif","method":"pokeradar","timer":"00h:00m:00s","counter":0,"shinycharm":false}
这几乎是(gif URL 没有放宠物小精灵的名字,但这是我稍后会解决的另一个问题)正是我在 json 文件,但代码无法创建此 json 文件。
我能够解决这个问题。
我把ipcMain.on("create-document-trigger",(hunt)=>{
换成了ipcMain.on("create-document-trigger",(event,data) =>{
现在代码看起来像这样并且可以工作
ipcMain.on("create-document-trigger",(event,data) =>{
dialog.showSaveDialog(mainWindow,{
filters: [{name:"json files",extensions:["json"]}]
}).then(({filePath}) =>{
console.log("filepath: "+filePath);
fs.writeFile(filePath,data,(err)=>{
if(err){
console.log(err.message);
}
})
})
})