exports.main = async (event, context) => { var PROTO_PATH = __dirname + '/protos/route_guide.proto' ; var async = require( 'async' ); var fs = require( 'fs' ); var parseArgs = require( 'minimist' ); var path = require( 'path' ); var _ = require( 'lodash' ); var grpc = require( 'grpc' ); var protoLoader = require( '@grpc/proto-loader' ); var packageDefinition = protoLoader.loadSync( PROTO_PATH, { keepCase: true , longs: String, enums: String, defaults: true , oneofs: true }); var routeguide = grpc.loadPackageDefinition(packageDefinition).routeguide; var client = new routeguide.RouteGuide( '打码的ip地址:50051' , grpc.credentials.createInsecure()); var COORD_FACTOR = 1e7; var point1 = { latitude: 409146138, longitude: -746188906 }; client.getFeature(point1, function (err, feature){ if (err){ return err; } else { return true ; } }); return {ok:1} } |
上面是的云函数代码,云端测试后,报错返回接口如下:
{"errorCode":1,"errorMessage":"user code exception caught","stackTrace":"channel.createCall is not a function"} |
为什么会这样?grpc接口,在本地测试是成功的。但是放到云函数上就不成功了。
调用日志如下:
调用日志 START RequestId: 2977fa93-c8e2-11e9-855a-525400dee61a Event RequestId: 2977fa93-c8e2-11e9-855a-525400dee61a TypeError: channel.createCall is not a function at getCall (/var/user/node_modules/grpc/src/client_interceptors.js:655:22) at /var/user/node_modules/grpc/src/client_interceptors.js:788:16 at /var/user/node_modules/grpc/src/client_interceptors.js:1397:14 at _buildChain (/var/user/node_modules/grpc/src/client_interceptors.js:1400:33) at Object.getInterceptingCall (/var/user/node_modules/grpc/src/client_interceptors.js:1362:10) at ServiceClient.Client.makeUnaryRequest (/var/user/node_modules/grpc/src/client.js:568:47) at ServiceClient.method_func (/var/user/node_modules/grpc/src/client.js:1000:43) at EventHandler.exports.main [as realHandler] (/var/user/index.js:46:10) at EventHandler.handle (/var/runtime/node8/bootstrap.js:323:28) at invoke (/var/runtime/node8/bootstrap.js:128:22) END RequestId: 2977fa93-c8e2-11e9-855a-525400dee61a Report RequestId: 2977fa93-c8e2-11e9-855a-525400dee61a Duration:2ms Memory:256MB MaxMemoryUsed:39.062500MB |
然后追查到client_interceptors.js getCall代码显示是这样:
有用云端安装依赖上传吗