The parameters passed to your callback are fs.Stats objects, which
unfortunately do not contain a filename, so you need to pass one along
yourself. Your second approach was the right idea, but implemented with a
common asynchronous code bug - by the time your callback is called, i is
now equal to fileNames.length, so fileNames[i] is undefined. You probably
want something which binds the filename to your callback:
for (i=0; i<fileNames.length; i++) {
fs.watchFile(filename, doAll.bind(null, fileNames[i]));
}
or creates a new scope for every step of the loop:
fileNames.forEach(function(filename) {
fs.watchFile(filename, function() {
doAll(filename);
});
});
On Wednesday, November 25, 2015 at 5:48:17 PM UTC-8, Anirban Bhattacharya
wrote:
>
> I also Tried the below option
>
> for(i=0;i<fileNames.length;i++)
> {
> console.log(fileNames[i]);
> fs.watchFile(fileNames[i],function(prev,cur){
> console.log(fileNames[i]);
> doAll(fileNames[i]);
> });
> }
> function doAll(fileName)
> {
>
> console.log(fileName);
> }
>
> But the console.log() in callback printing undefined.
> --------------------------------------------
> On Tue, 24/11/15, Anirban Bhattacharya <[email protected]
> <javascript:>> wrote:
>
> Subject: How to pass/get fileName in callBack of watchFile
> To: "nodejs" <[email protected] <javascript:>>
> Cc: "Anirban Bhattacharya" <[email protected] <javascript:>>
> Date: Tuesday, 24 November, 2015, 10:22 AM
>
> HI,
>
> I have to watch multiple files, and I don't want to
> create seperate callback function for each.
>
> fs.watchFile(testFile,callBack);
> function callBack(cur,prev) {
> console.log("in Test..");
> var data='';
> }
>
> How can I get the fileName from the callBack function?
>
> Thanks,
> Anirban
>
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/8c941dd4-84d9-4d12-856a-9d3621ab564a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.