【问题标题】:google-protobuf - Can't find variable: protogoogle-protobuf - 找不到变量:proto
【发布时间】:2022-11-10 06:22:25
【问题描述】:

我在 React Native (v0.65.1) 中使用 google-protobuf (v3.21.0) 时遇到问题,以便使用 expressif (Security1) 通过 SoftWiFi 连接设备 (IoT)

使用curve25519成功创建私钥和公钥后,我需要使用protobuf来传递有效负载。为此,我遵循以下解释: https://medium.com/@singhandresh09/using-protobuf-in-react-native-7cc8286389f5

在使用 protoc 编译器成功创建 js 文件后,我成功地使用了该应用程序。

我得到的错误是指由 protoc session_pb.js 创建的文件:

Can't find variable: proto

goog.object.extend(proto, sec0_pb)

在这里复制它就是我所做的。

  • 首先导入创建的js文件。 var session_pb = require('../../../proto/session_pb');

然后使用它

   const s = new session_pb.S0SessionCmd();
   const r = s.setClientPubKey('123');

我注意到在创建的 js 文件中,“proto”没有在任何地方定义。这里生成的文件:

...
// GENERATED CODE -- DO NOT EDIT!
/* eslint-disable */
// @ts-nocheck

var jspb = require('google-protobuf');
var goog = jspb;
var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);

var sec0_pb = require('./sec0_pb.js');
goog.object.extend(proto, sec0_pb);   <--- here is where the error occurs
var sec1_pb = require('./sec1_pb.js');
goog.object.extend(proto, sec1_pb);
var sec2_pb = require('./sec2_pb.js');
goog.object.extend(proto, sec2_pb);
...

【问题讨论】:

  • 现在遇到同样的问题,请更新发现!

标签: javascript react-native protocol-buffers proto


【解决方案1】:

我遇到了同样的问题并通过禁用metro.config.js 中的inlineRequires 来解决它。

{
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多