【问题标题】:goog is not defined error while trying to use Protocol Buffers - Google's data interchange format尝试使用协议缓冲区时 goog 未定义错误 - Google 的数据交换格式
【发布时间】:2016-04-05 05:57:50
【问题描述】:

我正在尝试使用 Protocol Buffers - Google 的数据交换格式,引用 https://github.com/google/protobuf/tree/master/js

我尝试按照文档进行操作,我能够设置协议编译器并能够做到

 protoc --js_out=library=myproto_libs,binary:. messages.proto 

但是当我执行 npm install google-protobuf 时,我得到了

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/google-
npm ERR! 404
npm ERR! 404 'google-protobuf' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

如果我编写的示例可以使用生成的二进制文件,它会说 goog 没有定义

goog.require('com.nec.eva.msap.proto.alerts.FaceAlert');
var message = com.nec.eva.msap.proto.alerts.FaceAlert();
message.watchlist_id("1");
message.candidate_id("25");
message.gender("female");
bytes = message.serializeBinary();

我不知道为什么我不能进行 npm install 以及 goog.require('com.nec.eva.msap.proto.alerts.FaceAlert');将使用生成的二进制文件请帮助我几乎花了一天时间理解但没有得到它

【问题讨论】:

    标签: node.js module npm protocol-buffers


    【解决方案1】:

    有问题和

    API 还是 not well-documented

    你需要用protoc messages.proto --js_out=import_style=commonjs,binary:.编译它

    或者设置

    var jspb = require('google-protobuf')
    var goog = jspb
    

    【讨论】:

      【解决方案2】:

      最好在http://node-modules.com/search?q=protocol+buffers 上搜索。这个很受欢迎,应该可以工作:https://github.com/dcodeIO/protobuf.js

      【讨论】:

        【解决方案3】:

        简单地说,google-protobuf 不在 npm 存储库中,因此安装它将不起作用(请参阅https://www.npmjs.com/package/google-protobuf)。但是,您可以尝试许多协议缓冲区库:

        【讨论】:

        • 你能说一下文档中的 goog 裁判是什么
        • 另外请说一下serializeBinary和encode有什么区别,即为什么要使用一个
        • 对于现在遇到这个问题的任何人来说,自从写下这个答案以来,这似乎已经改变了。有一个 google-protobuf 包,答案中的链接现在路由到该包,并且在 Google 自己的 javascript protobuf 存储库中明确引用:github.com/protocolbuffers/protobuf/tree/master/…
        猜你喜欢
        • 2022-01-14
        • 1970-01-01
        • 2012-12-01
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多