Javascript 节点插件api调用js对象';s方法失败
我将JavaScript对象传递给C++代码,C++代码调用这个对象的方法“Read DATA1”,但是对象的第二个方法(Read DATA2)没有调用;p> 控制台告诉我“readdata2”未定义 js代码:Javascript 节点插件api调用js对象';s方法失败,javascript,node.js,node-addon-api,Javascript,Node.js,Node Addon Api,我将JavaScript对象传递给C++代码,C++代码调用这个对象的方法“Read DATA1”,但是对象的第二个方法(Read DATA2)没有调用;p> 控制台告诉我“readdata2”未定义 js代码: const testfunc = require('./index').testfunc; class reader{ constructor(){ this.readdata2=function(){
const testfunc = require('./index').testfunc;
class reader{
constructor(){
this.readdata2=function(){
console.log('js:readdata2')
return 222
}
this.readdata1=function (){
console.log('js:readdata1')
let ret = this.readdata2()
return ret;
}
}
}
let n = testfunc(new reader());
console.log('n=',n)
c++代码:
Napi::Value testfunc(const Napi::CallbackInfo &info) {
LOG("C++:testfunc()!!");
Object r = info[0].As<Napi::Object>();
return r.Get("readdata1").As<Function>().Call({});
}
Napi::Value testfunc(const-Napi::CallbackInfo&info){
日志(“C++:testfunc()!!”);
对象r=info[0].As();
返回r.Get(“readdata1”).As().Call({});
}
输出:
C++:testfunc()!!
js:readdata1
/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17
let n = testfunc(new reader());
^
TypeError: Cannot read property 'readdata2' of undefined
at readdata1 (/Users/chentingfeng/cpp_proj/decodeDLL/test.js:11:28)
at Object.<anonymous> (/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17:9)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
npm ERR! Test failed. See above for more details.
Process finished with exit code 1
C++:testfunc()!!
js:readdata1
/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17
设n=testfunc(新读取器());
^
TypeError:无法读取未定义的属性“readdata2”
在readdata1(/Users/chentingfeng/cpp_proj/decodeDLL/test.js:11:28)
反对。(/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17:9)
at模块编译(内部/modules/cjs/loader.js:688:30)
在Object.Module.\u extensions..js(internal/modules/cjs/loader.js:699:10)
在Module.load(内部/modules/cjs/loader.js:598:32)
在tryModuleLoad(内部/modules/cjs/loader.js:537:12)
at Function.Module.\u load(内部/modules/cjs/loader.js:529:3)
位于Function.Module.runMain(内部/modules/cjs/loader.js:741:12)
启动时(内部/bootstrap/node.js:285:19)
在bootstrapNodeJSCore(internal/bootstrap/node.js:739:3)
npm错误!测试失败。请参见上文了解更多详细信息。
进程已完成,退出代码为1