Q:

node.js addon - how to pass a string parameter to (nan) C++

My node.js code does this :
var MyMQ = require( './build/release/mqconn' ) ;
var MyQmgrName = 'QM_CNT' ; // req.params.qmgrname ; 
MyMQ.connect ( MyQmgrName, function ( err, result ) { 

And my C++ code (uning nan) wants to receive the first string parameter :
NAN_METHOD( MQ_Connect ) {

    NanScope();
    Local<Value> szQMN( args[ 0 ] );
    printf( "(cc)>>>> qmn [%s].\n", szQMN ) ;

... but what I get is garbage.
Any clue on what am I doing wrong ?
Sebastian.

A:

First you should generally validate your arguments. Then you can get a string by calling ToString() on the argument. For example:
NAN_METHOD(MQ_Connect) {
  NanScope();

  if (args.Length() > 0) {
    if (args[0]->IsString()) {
      String::Utf8Value str(args[0]->ToString());
      printf("(cc)>>>> qmn [%s].\n", (const char*)(*str));
    }
  }

