admin管理员组

文章数量:1336304

in nodejs I want to read some data into an array which has been saved previously to file. The original data is structured in an array. Example:

let arr = [
  'id-001': [ '123', '246', '234' ],
  'id-002': [ '789', '235' ],
  ... and so on
];

It is saved like this:

fs.writeFileSync(dirpath + 'data.txt', arr);

This part works fine. The file contains this string:

[
  'id-001': [ '123', '246', '234' ],
  'id-002': [ '789', '235' ],
  ... and so on
]

Now I thought I could just read the file

let data = fs.readFileSync(filePath, 'utf8');

...that works...

and do something like

eval('let arr = ' + data);

or

let arr = eval(data);

or even

const vm = require('vm')
let arr = vm.runInNewContext(arr, {data})

and some more. Nothing works. It behaves strange, just nothing seems to happen, code after the eval seems not to be executed. No errors.

What may be wrong? Is there a better way?

in nodejs I want to read some data into an array which has been saved previously to file. The original data is structured in an array. Example:

let arr = [
  'id-001': [ '123', '246', '234' ],
  'id-002': [ '789', '235' ],
  ... and so on
];

It is saved like this:

fs.writeFileSync(dirpath + 'data.txt', arr);

This part works fine. The file contains this string:

[
  'id-001': [ '123', '246', '234' ],
  'id-002': [ '789', '235' ],
  ... and so on
]

Now I thought I could just read the file

let data = fs.readFileSync(filePath, 'utf8');

...that works...

and do something like

eval('let arr = ' + data);

or

let arr = eval(data);

or even

const vm = require('vm')
let arr = vm.runInNewContext(arr, {data})

and some more. Nothing works. It behaves strange, just nothing seems to happen, code after the eval seems not to be executed. No errors.

What may be wrong? Is there a better way?

Share Improve this question asked Jul 5, 2019 at 13:35 R0bynR0byn 4231 gold badge6 silver badges18 bronze badges 8
  • The let arr = … code at the beginning cannot work because it’s not valid JavaScript syntax. – Sebastian Simon Commented Jul 5, 2019 at 13:38
  • @sebastianSimon I guess its rather that arr.toString() is not valid JS ... – Jonas Wilms Commented Jul 5, 2019 at 13:45
  • @JonasWilms It would be valid, if the syntax errors would be corrected. – Sebastian Simon Commented Jul 5, 2019 at 13:47
  • I must be missing something ... Which syntax error? – Jonas Wilms Commented Jul 5, 2019 at 13:48
  • 1 Oh right, then the OP is also wrong about the files contents ... The whole question then doesnt make any sense ... – Jonas Wilms Commented Jul 5, 2019 at 13:53
 |  Show 3 more ments

3 Answers 3

Reset to default 5

You can parse the file content using JSON.parse after reading it, that should make it work.

Also, you'll want to persist a JSON.stringifyed representation of your data.

Here's a minimum example that shows the whole process:

const fs = require('fs');

function write(array, path) {
    fs.writeFileSync(path, JSON.stringify(array));
}

function read(path) {
    const fileContent = fs.readFileSync(path);
    const array = JSON.parse(fileContent);
    return array;
}

write(['a', 'b'], '/my/path/test.txt');
const arr = read('/my/path/test.txt');
console.log(arr);

When you write the file you should stringify it:

fs.writeFileSync(dirpath + 'data.txt', JSON.stringify(arr));

Then you simply need to parse the string data from the file back to a javascript object

let data = JSON.parse(fs.readFileSync(filePath, 'utf8'));

Correct your array 1st and write/read the file

const fs = require('fs');

let dir = './data.txt';

let arr  = {'id-001': [ '123', '246', '234' ],
             'id-002': [ '789', '444' ]};

fs.writeFileSync(dir, JSON.stringify(arr));

let data = fs.readFileSync(dir, 'utf8');

console.log(data);

本文标签: How to save an array to file and later read it into an array variable in nodejsJavaScriptStack Overflow