views:

42

answers:

2

I want to detect changes for a file, if the file changes, I will use child_process to execute a scp command to copy the file to a server.I looked up node.js documentation, the fs.watchFile function seems do what I want to do, but when I tried it, somehow it just doesn't work as I expected. The following code were used:

var fs = require('fs');                                                                        

console.log("Watching .bash_profile");

fs.watchFile('/home/test/.bash_profile', function(curr,prev) {
    console.log("current mtime: " +curr.mtime);
    console.log("previous mtime: "+prev.mtime);
    if (curr.mtime == prev.mtime) {
        console.log("mtime equal");
    } else {
        console.log("mtime not equal");
    }   
});

With above code, if I access the watched file, the callback function get execute, it will output the same mtime, and always output "mtime not equal" (I only accessing the file). Outputs:

Watching .bash_profile
current mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
previous mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
mtime not equal

Anybody know why the if statement failed(also tried using === identify check, but still get the same output) when the two mtime are the same?

+2  A: 

If mtime properties are Date objects, then these can never be equal. In JavaScript two separate objects are equal ONLY if they are actually the same object (variables are pointing to the same memory instance)

obj1 = new Date(2010,09,27);
obj2 = new Date(2010,09,27);
obj3 = obj1; // Objects are passed BY REFERENCE!

obj1 != obj2; // true, different object instances
obj1 == obj3; // true, two variable pointers are set for the same object
obj2 != obj3; // true, different object instances

To check if these two date values are the same, use

curr.mtime.getTime() == prev.mtime.getTime();

(I'm not actually sure if this is the case since I didn't check if watchFile outputs Date objects or strings but it definitely seems this way from your description)

Andris
Thanks, I didn't realized that mtime property is a object(I thought they are string object:( I used typeof curr.mtime, it return object, Is there a way in javascript to find out what kind of object?. In the example you given, I thought obj1 == obj2, because you only change the equality check, its only false when they do the identify check obj1 ==== obj2.
vito huang
You can use the operator `instanceof` like this: `var d = new Date(); if(d instanceof Date == true)alert(1);` or alternatively to get the textual object type use `var d = new Date(); Object.prototype.toString.call(d); // "[object Date]"`
Andris
One thing that I notices is that, if I do something like: __var i = 10; if (i instanceof Number) alert(1);__ the if statement never true(typeof i => "number" and Object.prototype.toString.call(i) => "[object Number]", however if I declare i as: var i = Number(10), it works. Anyone know the reason behind it?
vito huang
`10` is not an object but literal and `new Number(10)` is a object - `instanceof` works only with objects. If you check with `typeof` then `typeof 10 == "number"` but `typeof new Number(10) == "object"`. So as `new Number(10)` is an object, the `instanceof` operator starts to work.
Andris
A: 

quick and nasty solution. If you are not doing a date comparison in terms of previous or after (< or >) and you are simply comparing the datestrings, just do a quick toString() on each one.

 if (curr.mtime.toString() == prev.mtime.toString()) 
dryprogrammers