fs.writeFile is asynchronous. You need to pass a callback and read the file
from the callback:
fs.writeFile(__dirname + '/' + filename, JSON.stringify(data),
function(err) {
if (err) cb(err);
fs.readFile(__dirname + '/' + filename, function (err, data) {
if (err) cb(err);
console.log(data);
});
});
On Sunday, April 1, 2012 10:25:29 PM UTC+2, Matthew Hazlett wrote:
>
> This throws an error unexpected end of file.
>
> fs.readFile(__dirname + '/' + filename, function (err, data) {
> if (err) throw err;
> console.log(data);
> });
>
> This is a oneline file created by:
>
> fs.writeFile(__dirname + '/' + filename, JSON.stringify(data));
>
> The file exists and the permissions are fine. Whats wrong?
>
>
On Sunday, April 1, 2012 10:25:29 PM UTC+2, Matthew Hazlett wrote:
>
> This throws an error unexpected end of file.
>
> fs.readFile(__dirname + '/' + filename, function (err, data) {
> if (err) throw err;
> console.log(data);
> });
>
> This is a oneline file created by:
>
> fs.writeFile(__dirname + '/' + filename, JSON.stringify(data));
>
> The file exists and the permissions are fine. Whats wrong?
>
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en