Javascript 如何使用fs在node.js express上将数组正确写入文件?
我试图在node.js和 当我发送数组时,文件如下所示:[object] 当我在Javascript 如何使用fs在node.js express上将数组正确写入文件?,javascript,arrays,json,node.js,express,Javascript,Arrays,Json,Node.js,Express,我试图在node.js和 当我发送数组时,文件如下所示:[object] 当我在JSON.stringify(myArr)中发送数组时,它会正确地写入文件,但数据会损坏并转换为对象 json: [{ "name" : "BigTitleLine1", "content" : "APP TITLE 1" }, { "name" : "BigTitleLine2", "content" : "APP TITLE 2" }]; var express = requ
JSON.stringify(myArr)
中发送数组时,它会正确地写入文件,但数据会损坏并转换为对象
json:
[{
"name" : "BigTitleLine1",
"content" : "APP TITLE 1"
}, {
"name" : "BigTitleLine2",
"content" : "APP TITLE 2"
}];
var express = require('express'),
fs = require('fs'),
bodyParser = require('body-parser'),
app = express();
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.put('/update', function (req, res) {
console.log(req.body);
fs.writeFile("./json/test.json", req.body, function(err) {
res.json({ success: true });
});
// this returns true data on console
// but it writes [object Object],[object Object] to the file
var jsonData = JSON.stringify(req.body);
console.log(jsonData);
fs.writeFile("./json/test.json", jsonData, function(err) {
res.json({ success: true });
});
// this way writes well but
// it corrupts data and convert it to object:
//{"0":{"name":"BigTitleLine1","content":"APP TITLE 1"},"1":{"name":...}}
});
var server = app.listen(3000);
node.js:
[{
"name" : "BigTitleLine1",
"content" : "APP TITLE 1"
}, {
"name" : "BigTitleLine2",
"content" : "APP TITLE 2"
}];
var express = require('express'),
fs = require('fs'),
bodyParser = require('body-parser'),
app = express();
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.put('/update', function (req, res) {
console.log(req.body);
fs.writeFile("./json/test.json", req.body, function(err) {
res.json({ success: true });
});
// this returns true data on console
// but it writes [object Object],[object Object] to the file
var jsonData = JSON.stringify(req.body);
console.log(jsonData);
fs.writeFile("./json/test.json", jsonData, function(err) {
res.json({ success: true });
});
// this way writes well but
// it corrupts data and convert it to object:
//{"0":{"name":"BigTitleLine1","content":"APP TITLE 1"},"1":{"name":...}}
});
var server = app.listen(3000);
我正在尝试将数组写入文件。这应该可以正常工作:
var express = require('express'),
fs = require('fs'),
bodyParser = require('body-parser'),
app = express();
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.put('/update', function (req, res) {
// convert object to array
var arr = []
for (var index in req.body){
arr.push(req.body[index])
}
var jsonData = JSON.stringify(arr, null, 2);
console.log(jsonData);
fs.writeFile("./json/test.json", jsonData, function(err) {
res.json({ success: true });
});
});
var server = app.listen(3000);
整理数据应该是可行的。您是否尝试过删除第一个
fs.writeFile
,即仅尝试使用第二个fs.writeFile
(您正在对有效负载进行字符串化)。因为现在你有两个异步写语句,它们正在创建一个可能导致奇怪事情发生的竞争条件。谢谢@danillouz的帮助,但我不明白,你能写下来吗:?我遗漏了什么吗?为什么需要循环?你好,伙计,这是因为node.js认为我的模型是一个对象,但它应该认为是数组@danillouzOh,我想我现在明白了,这是因为角度模型(类似数组的对象)。酷,谢谢@BarlasApaydin,你有一个非常棒的网站,顺便说一句:)嗨,伙计,我还有一个问题,我如何压缩这个输出并从你创建的json数组中删除空白?更改json.stringify(arr,null,2);到JSON.stringify(arr);