我正在创建并从一个从c ++节点插件调用的函数返回一个javascript函数对象。该对象已创建,但未被该函数返回。
回调函数,用于创建名为cursor的对象
var callback_fn = function(record){
var res = new cursor(records);//functionalities of res(created here are working here when tested inside callback_fn but its not getting returned)
return res;//not only object, nothing was getting returned.I even tried returning a dummy var.
}
调用“callback_fn”的节点插件片段(在args [1]中)
//Isolate* isolate = Isolate::GetCurrent();
//HandleScope scope(isolate);
Local<Object> obj= Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate,"name"),Integer::New(isolate,custnum));
obj->Set(String::NewFromUtf8(isolate,"address"),Integer::New(isolate,custnum));//integer is assigned to name and address just for testing.
obj->Set(String::NewFromUtf8(isolate,"custnum"),Integer::New(isolate,custnum));
Local<Function> cb = Local<Function>::Cast(args[1]);
Local<Value> argv[1] = {obj};
cb->Call(isolate->GetCurrentContext()->Global(), 1, argv);
答案 0 :(得分:0)
代码中的错误是javascript函数“callback_fn”将对象返回到cpp插件。因此,我们需要将它存储在cpp插件中并返回它,以便实现回调。
ret_val = cb->Call(isolate->GetCurrentContext()->Global(), 1, argv);
//where ret_val is defined as Local<value> ret_val;
args.GetReturnValue().Set(ret_val);
在javscript中存储并返回。
var res_final=addon.addonfunction(input to addon);
return res_final;
如果写入的cpp插件不是异步的,我们可以在节点程序中将res存储一个单独的var,并在addon.addonfunction(输入到addon)完成执行后返回它(因为所有内容都按顺序执行同步程序)。