Rax Wunter Rax Wunter - 1 year ago 223
C++ Question

Set a string to v8 Array

I investigate native Node addons using Nan.

So I'm trying to return back to Node an array of strings with the following code:

NAN_METHOD(open) {
Local<Array> v8Array = Nan::New<Array>();
std::string str = "erwer";
v8Array->Set(0, Nan::New<String>(str.c_str()) );
//v8Array->Set(0, Nan::New<Integer>(12) );

But it isn't compiled:

error: no matching function for call to ‘v8::Array::Set(int, Nan::imp::MaybeFactoryBase<v8::String>::return_t)’
v8Array->Set(0, Nan::New<String>(str.c_str()) );

But adding an item as integer (commented line) works properly.
Why can't I add a string to array?

Answer Source

So, finally I realised that the second argument in case of being a String should be wrapped with ToLocalChecked():

v8Array->Set(i, Nan::New<String>(results[i].c_str()).ToLocalChecked() );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download