c++node.jsnode.js-addonn-api

N_API How to send int value parameters to Napi::CallbackInfo


This My first node.js and n_api. I have been using PHP/APACHI. But I need the c++ library for my web And I decided to using n_api. The problem is that the value sent by ajax is always 0 in c++. I don't know what is problem. ex) I using a vscode.

const testAddon = require('./build/Release/firstaddon.node');

var http = require('http');
var url = require('url');
var fs = require('fs');
const express = require('express');
const app = express();
bodyParser = require('body-parser');

var port = '1080';

app.use(bodyParser.json());         // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

app.post('/server', function(req, res){ 
    var responseData = {};
    responseData.pID = req.body.pID; 
    console.log(responseData.pID);              <<============= here, value is correct.
    const prevInstance = new testAddon.ClassExample(4.3);
    var value = prevInstance.getFile(responseData.pID);   
    console.log(value);
    res.json(responseData);
});

If ajax sends 2, console.log(responseData.pID) //2 appears. It is normal. Below is the classtest.cpp

Napi::Value ClassTest::GetFile(const Napi::CallbackInfo &info)
{
  Napi::Env env = info.Env();
  Napi::HandleScope scope(env);

  using namespace std;
  if (info.Length() != 1 || !info[0].IsNumber())
  {
    Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
  }   
  Napi::Number file_id = info[0].As<Napi::Number>();
  int num = this->actualClass_->openFile(file_id);                   <<== here, file id

  return Napi::Number::New(info.Env(), num);
}

And ActualClass.cpp Files showing problems.

int ActualClass::openFile(int id)
{
  ifstream fin;  
  cout << id << endl;                  <<============================ here, always '0'

  filename += to_string(id) += ".txt";
  fin.open(filename.c_str(), ios_base::in | ios_base::binary);

  if (fin.is_open())
  {
    while (fin.read((char *)&sdo, sizeof(sdo)))
    {
      cout << setw(20) << sdo.name << ":"
           << setprecision(0) << setw(12) << sdo.width
           << setprecision(2) << setw(6) << sdo.height
           << setprecision(4) << setw(6) << sdo.size << endl;
      slist.push_back(sdo);
    }
    fin.close();       
  }
  else if (!fin.is_open())
  {
    cerr << "can't open file " << filename << ".\n";
    exit(EXIT_FAILURE);
  }

  return sdo.size;
}

Only files 1 to 4 are prepared. But, the argument value entering the function is always 0. Result is "can't open file 0.txt". How can I solve it?

Napi::Number file_id = info[0].As<Napi::Number>();

I know here it is converted to an int value that can be handled by C++. Is there anything else I don't know? Thanks for reading.


Solution

  • You need to cast it to the number with the help of the Napi::Number::Int32Value call. (You can also use Napi::Number::Int64Value for bigger numbers) Try this.

    int file_id = info[0].ToNumber().Int32Value();
    

    Also unrelated to the question, but worth mentioning that when you are doing ThrowAsJavaScriptException() the actual C++ code keeps executing, you better return undefined to avoid nasty bugs.

      if (info.Length() != 1 || !info[0].IsNumber())
      {
        Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
        return Env.Undefined();
      } 
    

    A more clean way will be to enable the CPP exceptions and just throw the error in that place.

      if (info.Length() != 1 || !info[0].IsNumber())
      {
        throw Napi::TypeError::New(env, "Number expected");
      }