[英]Setting a Node.js Object to data read from a file
I want to read data from a file and add it to an Object stored in memory. 我想从文件读取数据并将其添加到存储在内存中的对象中。 The data in the file text.txt looks roughly like this:
文件text.txt中的数据大致如下所示:
One: {title: 'One' ,
contributor: 'Fred',
summary: 'blah' ,
comments: 'words' },
Two: {title: 'Two' ,
contributor: 'Chris' ,
summary: 'blah blah i'm a blah' ,
comments: '' },
I'm trying to set it to an empty Object like so: 我正在尝试将其设置为一个空对象,如下所示:
var fs = require('fs');
var text = Object.create(null);
fs.readFile("./public/text.txt", "utf-8", function(error, data) {
text = { data };
});
However, when I log text
to the console, it comes out looking like this: 但是,当我将
text
记录到控制台时,它看起来像这样:
{ data: 'One: {title: \'One\' ,\ncontributor: \'Fred\',\nsummary: \'blah\' ,\ncomments: \'words\' },\n \nTwo: {title: \'Two\' ,\ncontributor: \'Chris\' ,\nsummary: \'blah blah i\'m a blah\' ,\ncomments: \'\' },\n\n' }
Apparently, it's reading data
as a key. 显然,它正在读取
data
作为密钥。 What I really want, though, is something more like so: 不过,我真正想要的是这样的东西:
{
One: {title: 'One' ,
contributor: 'Fred',
summary: 'blah' ,
comments: 'words' },
Two: {title: 'Two' ,
contributor: 'Chris' ,
summary: 'blah blah i'm a blah' ,
comments: '' },
}
Any advice here would be much appreciated. 这里的任何建议将不胜感激。
If you are using a newer version of Node, then you have support for ES6. 如果使用的是较新版本的Node,则支持ES6。
// So your code
`text = { data }`
// is actually a shortcut for
`text = { data: data }`
That's why you end up with an object that has the key data and the value is a string version of what was found in the file. 这就是为什么您最终得到一个具有键数据的对象,并且该值是该文件中找到的字符串版本的原因。 Instead, just use JSON.parse on the data parameter (which is a string) and it'll convert it to an Object, which you can store in text.
相反,只需在data参数(这是一个字符串)上使用JSON.parse,它将把它转换为一个对象,您可以将其存储为文本。 Like this
像这样
var fs = require('fs');
var text = Object.create(null);
fs.readFile("./public/text.txt", "utf-8", function(error, data) {
text = JSON.parse(data);
});
You'll also need to make the file valid json. 您还需要使文件有效json。 Which means keys need quotes around them as do String values.
这意味着键和字符串值一样需要在键周围加上引号。
{
"One": {
"title": "One" ,
"contributor": "Fred",
"summary": "blah" ,
"comments": "words"
},
"Two": {
"title": "Two" ,
"contributor": "Chris" ,
"summary": "blah blah i'm a blah" ,
"comments": ""
}
}
What you are trying to do is use eval
, which is the only way if you really don't want to edit the file to be valid JSON or to export an object as @Spidy suggested. 您试图做的是使用
eval
,这是您真的不想将文件编辑为有效JSON或按@Spidy建议导出对象的唯一方法。 Just be sure the file is valid JavaScript, because the example you gave had 只需确保文件是有效的JavaScript,因为您提供的示例具有
summary: 'blah blah i'm a blah'
but you need to escape i'm
like i\\'m
. 但是你需要逃避
i'm
就像i\\'m
。
var fs = require('fs');
var text = {};
fs.readFile('./public/text.txt', 'utf-8', function (error, data) {
eval(`text = {${data}}`);
//eval('text = {' + data + '}');
});
But I wouldn't necessarily recommend that because that allows arbitrary javascript to get executed. 但我不一定建议这样做,因为这样可以执行任意JavaScript。 Depending on how the data in the file gets there it would be a huge security risk.
根据文件中数据如何到达那里,将存在巨大的安全风险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.