Search code examples
node.jsmonodevelop

Why am I getting this error? r1= buf1.compare(buf2); ^ TypeError: Object Rahul has no method 'compare'


/*This a demo program to campare buffers to each other. The expected output should be some numbers so that we came to know that these are equal or not.
taking three buffer variables and storing some data into it.
*/

var buf1 = new Buffer('Rahul');  Buffer contains Rahul
var buf2 = new Buffer('Kumar');  Buffer contains Kumar
var buf3 = new Buffer('Rahul');  Buffer contains Rahul

//now comparing these buffers to each other

r1= buf1.compare(buf2); 
r2= buf1.compare(buf3); 
r3= buf2.compare(buf3);  

//Printing them to console

console.log(r1+ " " + r2 + " " + r3);

Solution

  • 'compare' is not a prototype function. It should be used as:

    Buffer.compare(b1,b2)
    

    Added code example:

    var b1=new Buffer("a");
    var b2=new Buffer("b");
    var r=Buffer.compare(b1,b2);
    console.log(r);