0

I delcare a function to create a file with path, and data as variable. here my code

const fse = require("fs-extra");
function createFile(name, contents) {
    fse.outputFile(name, contents, function(err) {

        }
    };

than

var name = "./path/file1";
var contents = "file1content";
createFile(name, contents);

name = "./path/file2";
contents = "file2content";
createFile(name, contents);

name = "./path/file3";
contents = "file3content";
createFile(name, contents);

file1, file2, file3 is created, but files's contents all are undefined is there any way to make my code possible?

thanks

Ngô Hữu Nam
  • 563
  • 6
  • 12

1 Answers1

0

Your function createFile has arguments as (name, contents) but why are you using different parameter names here fse.outputFile(fullPathFileName, first_contents.

Match them as below and try,

const fse = require("fs-extra");
function createFile(fullPathFileName, first_contents) {
    fse.outputFile(fullPathFileName, first_contents, function(err) {

    });

then,

var name = "./path/file1";
var contents = "file1content";
createFile(name, contents);

name = "./path/file2";
contents = "file2content";
createFile(name, contents);

name = "./path/file3";
contents = "file3content";
createFile(name, contents);
Aruna
  • 11,959
  • 3
  • 28
  • 42
  • Sorry, my mistake, I edited my own code before post here but not edit complete. Sorry for that. Re-editd my post. Thanks – Ngô Hữu Nam Nov 16 '16 at 05:21
  • Your function has the variables name same as the calling code. If they are in one file then they will create an issue. Try with different variables name as in my sample above – Aruna Nov 16 '16 at 05:23