C++ 在节点加载项中添加两个字符串参数

C++ 在节点加载项中添加两个字符串参数,c++,node.js,node-addon-api,C++,Node.js,Node Addon Api,我正在尝试在我的插件中添加两个字符串参数,但无法使其工作。我总是在两个参数的类型上出错 我的职能: void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); // Check the number of argume

我正在尝试在我的插件中添加两个字符串参数,但无法使其工作。我总是在两个参数的类型上出错

我的职能:

void Add(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  // Check the number of arguments passed.
  if (args.Length() < 2) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong number of arguments")
            .ToLocalChecked()));
    return;
  }

  // Check the argument types
  if (!args[0]->IsString() || !args[1]->IsString()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
    return;
  }

  Local<String> arg1 = args[0]->ToString(context).ToLocalChecked();
  Local<String> arg2 = args[1]->ToString(context).ToLocalChecked();

  args.GetReturnValue().Set(arg1 + arg2);
}
void添加(常量函数callbackinfo&args){
隔离*隔离=args.GetIsolate();
本地上下文=隔离->GetCurrentContext();
//检查传递的参数数。
if(args.Length()<2){
隔离->ThroweException(异常::类型错误(
字符串::NewFromUtf8(隔离,“参数数量错误”)
.ToLocalChecked());
返回;
}
//检查参数类型
如果(!args[0]->IsString()| |!args[1]->IsString()){
隔离->ThroweException(异常::类型错误(
字符串::NewFromUtf8(隔离,“错误参数”).ToLocalChecked();
返回;
}
本地arg1=args[0]->ToString(上下文).ToLocalChecked();
本地arg2=args[1]->ToString(上下文).ToLocalChecked();
args.GetReturnValue().Set(arg1+arg2);
}
整个文件:

#include <node.h>
#include <string>

namespace demo {

using v8::Context;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

void Add(const FunctionCallbackInfo<Value> &args) {...

void Init(Local<Object> exports) { NODE_SET_METHOD(exports, "add", Add); }

NODE_MODULE(NODE_GYP_MODULE_NAME, Init);

}
#包括
#包括
名称空间演示{
使用v8::Context;
使用v8::Exception;
使用v8::FunctionCallbackInfo;
使用v8::隔离;
使用v8::Local;
使用v8::Number;
使用v8::Object;
使用v8::String;
使用v8::Value;
void Add(常量函数callbackinfo&args){。。。
void Init(本地导出){NODE_SET_方法(导出,“添加”,添加);}
节点模块(节点模块名称,Init);
}

任何反馈都将不胜感激。

找到了解决方案。唯一的问题是我应该将
Local
类型转换为
std::string
。将它们相加后,在返回值时将它们转换回
Local

代码:

void添加(常量函数callbackinfo&args){
隔离*隔离=args.GetIsolate();
本地上下文=隔离->GetCurrentContext();
//检查传递的参数数。
if(args.Length()<2){
隔离->ThroweException(异常::类型错误(
字符串::NewFromUtf8(隔离,“参数数量错误”)
.ToLocalChecked());
返回;
}
//检查参数类型
如果(!args[0]->IsString()| |!args[1]->IsString()){
隔离->ThroweException(异常::类型错误(
字符串::NewFromUtf8(隔离,“错误参数”).ToLocalChecked();
返回;
}
v8::String::Utf8Value str1(隔离,参数[0]);
v8::String::Utf8Value str2(隔离,参数[1]);
std::字符串搜索词(*str1);
std::字符串体(*str2);
std::string res=searchWord+body;
args.GetReturnValue().Set(
字符串::NewFromUtf8(隔离,res.c_str()).ToLocalChecked();
}

什么是“它似乎不正常工作”真正的意思?它意味着我无法让它工作(我可能应该将它重新表述为更具描述性),我总是会在这两个参数的类型上出错。那么,给我们你所拥有的一切:重现问题所需的完整源代码,错误消息等等。正如你所意识到的,你必须更具描述性。如果有人给你自己的描述,你能帮助那个人吗?可能不会。这就是为什么没有人帮助你你在这里。我们就是不能。经过一些挖掘,我找到了解决办法。不过谢谢你的反馈!
void Add(const FunctionCallbackInfo<Value> &args) {
  Isolate *isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  // Check the number of arguments passed.
  if (args.Length() < 2) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong number of arguments")
            .ToLocalChecked()));
    return;
  }

  // Check the argument types
  if (!args[0]->IsString() || !args[1]->IsString()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
    return;
  }

  v8::String::Utf8Value str1(isolate, args[0]);
  v8::String::Utf8Value str2(isolate, args[1]);
  std::string searchWord(*str1);
  std::string body(*str2);

  std::string res = searchWord + body;

  args.GetReturnValue().Set(
      String::NewFromUtf8(isolate, res.c_str()).ToLocalChecked());
}