尝试找出是否可以使用原生代码(使用N-API)运行firebase云函数。我有一个简单的"hello world“示例,它在模拟器下工作得很好,但是当我尝试部署它时,我得到了INVALID_ARGUMENT错误:
status: {
code: 3
message: "INVALID_ARGUMENT"
}这可不是什么好消息。只是想知道有没有人能说明一下情况。谢谢!
下面是函数:
'use strict';
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest(async(request, response) => {
console.time('Program runtime');
const testAddon = require('bindings')('testaddon.node')
const {promisify} = require('util');
module.exports = testAddon;
const asyncCommand = testAddon.hello();
try {
const result = await asyncCommand;
console.log('CONTENT:', result);
response.send(result);
}
catch (err) {
console.log('ERROR:', err);
response.send('ERROR:', err);
}
console.timeEnd('Program runtime');
});和对应的C++源:
#include <napi.h>
namespace functionexample {
std::string hello();
Napi::String HelloWrapped(const Napi::CallbackInfo& info);
Napi::Object Init(Napi::Env env, Napi::Object exports);
}#include "functionexample.h"
std::string functionexample::hello(){
return "Hello World";
}
Napi::String functionexample::HelloWrapped(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::String returnValue = Napi::String::New(env, functionexample::hello());
return returnValue;
}
Napi::Object functionexample::Init(Napi::Env env, Napi::Object exports)
{
exports.Set(
"hello", Napi::Function::New(env, functionexample::HelloWrapped)
);
return exports;
}发布于 2020-02-22 00:23:44
似乎问题出在某个版本的节点引擎上。我已经切换到node10,而不是node8,我的测试函数部署正确,工作正常。
发布于 2020-02-20 19:20:26
我猜问题在于testaddon.hello()不返回承诺,所以等待它是一个问题。如果addon.hello()是一个异步javascript函数,那么javascript将确保它返回一个promise,但它是一个C++函数。
我以前没有使用过插件中的promises,但这可能会有所帮助:
https://github.com/nodejs/node-addon-api/blob/master/doc/promises.md
发布于 2020-03-09 19:25:08
从Node.js v8.6.0开始,N-API已经被标记为稳定的API,所以如果你使用Node.js运行时的早期版本,你可能会遇到这里报告的问题。这是因为切换到Node.js版本10都工作得很好。
https://stackoverflow.com/questions/60304282
复制相似问题