goog is not defined error while trying to use Protocol Buffers - Google's data interchange format

青春壹個敷衍的年華 提交于 2020-05-15 10:18:09

问题


I am trying to use Protocol Buffers - Google's data interchange format referring https://github.com/google/protobuf/tree/master/js

I tried to follow documentation i am able to get the setup of Protocol Compiler and able to do

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

but when i do npm install google-protobuf i am getting

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.

and if i write the sample which can use that generated binary it says goog is not defined

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();

I am not sure why i am not able to do npm install and also how that goog.require('com.nec.eva.msap.proto.alerts.FaceAlert'); will use the generated binary please help i have almost spent a day to understand but not getting it


回答1:


Best to search on http://node-modules.com/search?q=protocol+buffers. This one is popular and should work: https://github.com/dcodeIO/protobuf.js




回答2:


Simply, google-protobuf is not in the npm repository, so installing it will not work (see https://www.npmjs.com/package/google-protobuf). However, there are a number of protocol buffers libraries that you could try:

  • https://www.npmjs.com/package/protocol-buffers this one plays a bit fast and loose, but is really good for many simple cases (and it's blindingly fast).
  • https://www.npmjs.com/package/protobufjs aims to be a bit more complete; I haven't personally used this one, but it's well regarded and has a large number of downloads.



回答3:


There are issues and

the API is not well-documented yet

You need to compile it with protoc messages.proto --js_out=import_style=commonjs,binary:.

Or set

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


来源:https://stackoverflow.com/questions/36418217/goog-is-not-defined-error-while-trying-to-use-protocol-buffers-googles-data-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!