[英]fs.writefile with an object returns [object object]
I am trying to move data from a form to a json file, this is my first time doing something like this. 我正在尝试将数据从表单移动到json文件,这是我第一次这样做。 When it runs, my json file looks like this: 当它运行时,我的json文件如下所示:
[object Object] instead of displaying an email, username, and password. [object Object]而不是显示电子邮件,用户名和密码。
This is my server code(expressjs): 这是我的服务器代码(expressjs):
app.post('/action_page.php',function(req,res){
'use strict'
email = req.body.email;
username = req.body.name;
password = req.body.password;
loginData = {
email: email,
username: username,
password: password
};
fs.writeFile('data.json', loginData, finished);
res.sendFile(__dirname + '/signupResponse.html');
email, username, and password are global variables declared as empty strings, and logindata is also global declared as such: email,用户名和密码是声明为空字符串的全局变量,而logindata也是这样声明的全局变量:
var loginData = {"email": "","username": "","password": ""};
Any help would be greatly apreciated! 任何帮助将不胜感激!
That problem can be resolved by using the JSON.stringify
method to serialise the loginData
object to a string which is what fs.writeFile
expects for the second parameter. 通过使用JSON.stringify
方法将loginData
对象序列loginData
fs.writeFile
期望第二个参数的字符串,可以解决该问题。
You can do this by updating the following line like so: 您可以通过更新以下行来做到这一点:
fs.writeFile('data.json', JSON.stringify(loginData), finished);
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.