0

我有以下代码...

// robot_node.c
#include <stdio.h>
#include <string.h>
#include "robot_node.h"
#include "robot.h"

napi_value node_forward(napi_env env, napi_callback_info info){
    napi_value result;
    napi_status status;
    int answer = forward();
    status = napi_create_int64(env, answer, &result);
    if (status != napi_ok) return NULL;
    return result;
}
napi_value node_stop(napi_env env, napi_callback_info info){
    napi_value result;
    napi_status status;
    int answer = stop();
    status = napi_create_int64(env, answer, &result);
    if (status != napi_ok) return NULL;
    return result;
}
napi_value helloWorld(napi_env env, napi_callback_info info) {
  napi_value world;
  napi_status status;
  const char* str = "world";
  size_t str_len = strlen(str);
  status = napi_create_string_utf8(env, str, str_len, &world);
  if (status != napi_ok) return NULL;
  return world;
}

//module.c
#include "robot_node.h"
#include "node_common.h"

napi_value init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;
  napi_property_descriptor commands[] = {
    DECLARE_NAPI_PROPERTY("hello", helloWorld),
    DECLARE_NAPI_PROPERTY("forward", node_forward),
    DECLARE_NAPI_PROPERTY("stop", node_stop)
  };
  status = napi_define_properties(env, exports, 1, commands);
  if (status != napi_ok) return NULL;
  return exports;
}

//test.mjs
import Bind from 'bindings';

const bindings = Bind("robot-ui.node");
const result = bindings.hello();

console.log(`The result is ${result}`);
bindings.forward();
setTimeout(()=>{
  bindings.stop();
}, 1000);

当我运行 hello world 部分工作正常但前进和停止功能失败...

TypeError:bindings.forward 不是函数

我对 C 开发很陌生,不明白如何找到错误。为什么 hello world 函数工作正常但 forward 函数失败?

4

1 回答 1

0

哎呀才意识到

status = napi_define_properties(env, exports, 1, commands);

应该

status = napi_define_properties(env, exports, 3, commands);
于 2020-04-26T15:42:25.837 回答