我有一个非常奇怪的问题。我有一个后端 api 可以将 json 数据导入我的 mongodb。
在屏幕上,我有一个上传按钮来上传文件,为此我使用了 react-dropzone。例如认为我有一个像“db.json”这样的文件,在这个文件中有一个如下的 json
{
"datapointtypes":[
{"id":"Wall plug","features":[{"providesRequires":"provides","id":"Binary switch"},{"providesRequires":"requires","id":"Binary sensor","min":"1","max":"2"}],"parameters":[{"id":"Communication type","type":"Communication type"}],"functions":[{"id":"Electricity"},{"id":"Switch"}]},
{"id":"Door sensor","features":[{"providesRequires":"provides","id":"Binary sensor"}],"parameters":[{"id":"Communication type","type":"Communication type"}],"functions":[{"id":"Door"},{"id":"Sensor"}]}
],
"datatypes":[
{"id":"Communication type","type":"enum","values":[{"id":"Zwave"},{"id":"Zigbee"}]},
{"id":"Zigbee network address","type":"decimal","min":1,"max":65336,"res":1},
{"id":"Serial port","type":"string"}
],
"features":[
{"id":"Zwave receiver","exposedtype":"Zwave command","functions":[{"id":"Communication"}]},
{"id":"Zigbee receiver","exposedtype":"Zigbee command","functions":[{"id":"Communication"}]},
{"id":"Binary switch","exposedtype":"On off state","functions":[{"id":"Actuator"}]},
{"id":"Binary sensor","exposedtype":"On off state","functions":[{"id":"Sensor"}]}
],
"servicetypes":[
{"id":"Room controller","datapointtype":"Room controller","DelayActive":false,"DelayValue":""},
{"id":"Xiaomi door sensor","datapointtype":"Door sensor","parameters":[{"id":"Zigbee network address","type":"Zigbee network address"},{"id":"Zigbee node id","type":"Zigbee node id"}],"parametervalues":[{"id":"Communication type","value":"Zigbee"}]}
],
"systems":[
{"id":"system 2","services":[{"serviceType":"Room controller","id":"servis 1"}],"serviceRelations":[{"serviceName":"servis 1","featureName":"Binary sensor"}],"parametervalues":[{"id":"Delay","paramName":"Delay","serviceType":"Room controller","value":"binary"}]},
{"id":"system 3","services":[{"serviceType":"Room controller","id":"servis 1"}],"serviceRelations":[{"serviceName":"servis 1","featureName":"Binary sensor"}],"katid":"7"}
]
}
问题是这样的。如果浏览器控制台打开,那么我的代码运行成功,我可以将 json 数据导入我的 mongodb。但如果浏览器控制台关闭,我会收到“SyntaxError:JSON 输入意外结束”错误。
这是我在导入按钮上使用的功能
class FileUpload extends Component {
state = {
warning: ""
}
uploadFile = (files, rejectedFiles) => {
files.forEach(file => {
const reader = new FileReader();
reader.readAsBinaryString(file);
let fileContent = reader.result;
axios.post('http://localhost:3001/backendUrl', JSON.parse(fileContent),
{
headers: {
"Content-Type": "application/json"
}
})
.then(response => {
this.setState({warning: "Succeed"})
})
.catch(err => {
console.log(err)
});
});
}
render() {
return (
<div>
<Dropzone className="ignore" onDrop={this.uploadFile}>{this.props.children}
</Dropzone>
{this.state.warning ? <label style={{color: 'red'}}>{this.state.warning}</label> : null}
</div>
)
}
}
我做错了什么或者是什么原因造成的?你能帮助我吗?
谢谢