c++ - N_API 如何将 int 值参数发送到 Napi::CallbackInfo
问题描述
这是我的第一个 node.js 和 n_api。我一直在使用 PHP/APACHI。但我需要用于我的网络的 c++ 库我决定使用 n_api。问题是ajax发送的值在c++中总是0。我不知道有什么问题。例如)我使用 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);
});
如果 ajax 发送 2,console.log(responseData.pID) //2 就会出现。这是正常的。下面是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);
}
并且 ActualClass.cpp 文件显示问题。
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;
}
仅准备文件 1 到 4。但是,输入函数的参数值始终为 0。结果是“无法打开文件 0.txt”。我该如何解决?
Napi::Number file_id = info[0].As<Napi::Number>();
我知道这里它被转换为可以由 C++ 处理的 int 值。还有什么我不知道的吗?谢谢阅读。
解决方案
您需要在呼叫的帮助下将其转换为号码Napi::Number::Int32Value
。(您也可以Napi::Number::Int64Value
用于更大的数字)试试这个。
int file_id = info[0].ToNumber().Int32Value();
也与问题无关,但值得一提的是,当您执行ThrowAsJavaScriptException()
实际的 C++ 代码时,您最好返回 undefined 以避免讨厌的错误。
if (info.Length() != 1 || !info[0].IsNumber())
{
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return Env.Undefined();
}
一种更简洁的方法是启用 CPP 异常并在该位置抛出错误。
if (info.Length() != 1 || !info[0].IsNumber())
{
throw Napi::TypeError::New(env, "Number expected");
}
推荐阅读
- cordova - Ionic 3-如何在设备默认浏览器中打开外部 url
- c++ - 我的随机生成器是否无法正常工作,或者我在决定人类/骷髅是否击中对手时是否存在错误?
- sql-server - SQL Server Service Broker - 端点上的证书就足够了吗?
- node.js - Socket.io 丢包?
- php - 当我提交 php 表单时,PDF 不显示
- informatica - Informatica Developer - 加载多个平面文件
- android - 如何设置两个吐司之间的持续时间?
- angularjs - 使用 Angular 在数据库中注册现有电子邮件时如何引发警报错误?
- java - 具有配置数量的处理对象的 REST 流式传输 - 卸载内存
- service-worker - Service Worker 将 API 调用中的文件添加到预缓存