0

https://medium.com/@muehler.v/tutorial-to-node-js-native-c-modules-part-2-arrays-json-and-callbacks-9b81f09874cd

I am following along with the above article trying to access a string in an object that is passed to a C++ node addon function. My function is below but I am unable to get it the addon to compile. When I do I get the error:

../addon.cpp:239:26: error: no viable conversion from 'MaybeLocal<v8::Value>' (aka 'v8::MaybeLocal<v8::Value>') to 'v8::Local<v8::Value>'
    v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp);
                         ^          ~~~~~~~~~~~~~~~~~~~~~~

Any ideas on how I might resolve this error? Thanks

NAN_METHOD(init) {
  v8::Local<v8::Object> obj = info[0]->ToObject();
  v8::Local<v8::String> pwdProp = Nan::New("PWD").ToLocalChecked();

  std::string pwd = "";

  if (Nan::HasOwnProperty(obj, pwdProp).FromJust()) {
    v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp);
    pwd = std::string(*Nan::Utf8String(pwdValue->ToString()));
  }

  std::cout << pwd << std::endl;
}

1 Answer 1

1

Nan::Get returns a MaybeLocal, which might be empty for reasons mentioned in the docs.

You can get the Local handle from a MaybeLocal by using .ToLocalChecked() as follows: v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp).ToLocalChecked();.

To check whether the MaybeLocal actually holds a value, you can check bool isEmpty = Nan::Get(obj, pwdProp).IsEmpty().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.