node-addon-examples icon indicating copy to clipboard operation
node-addon-examples copied to clipboard

unable get string length

Open calidion opened this issue 7 years ago • 1 comments

I am now testing for echo function,

for code

napi_value Method(napi_env env, napi_callback_info args)
{
  // napi_value str;
  napi_status status;
  cout << "inside method!" << endl;

  size_t argc = 0;
  napi_value argv[1];
  status = napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr);

  if (status != napi_ok)
  {
    napi_throw_error(env, NULL, "Error Get Parameters!");
    return nullptr;
  }
  cout << "before argc!" << endl;

  if (argc < 1)
  {
    cout << "" << endl;
    return nullptr;
  }
  size_t strlen;
  status = napi_get_value_string_utf8(env, argv[0], NULL, 0, &strlen);
  if (status != napi_ok)
  {
    napi_throw_error(env, NULL, "Error Get String Length!");

    return nullptr;
  }
  // status = napi_create_string_utf8(env, argv[0], NAPI_AUTO_LENGTH, res);
  // if (status != napi_ok)
  // {
  //   napi_throw_error(env, NULL, "Error Get String!");
  //   return nullptr;
  // }
  return nullptr;
}

and

  status = napi_set_named_property(env, exports, "echo", fn);

and

 echo('hello!')

,

I got:

inside method!
before argc!
before get string!
�"�d2
    1) hello


  0 passing (11ms)
  1 failing

  1) crypto
       hello:
     Error: Error Get String Length!
      at Context.it (tests/1-test.js:4:24)

calidion avatar Jun 23 '18 10:06 calidion

What was the value of status for the call to napi_get_value_string_utf8?

mhdawson avatar Jan 30 '19 18:01 mhdawson