gRPC节点错误:JSON在位置0处出现意外令牌u [英] gRPC-Node Error: Unexpected token u in JSON at position 0

查看:74
本文介绍了gRPC节点错误:JSON在位置0处出现意外令牌u的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不断收到位置0的JSON中的意外令牌u"错误.我目前正在从主要发起方发出请求,该发起方正在向客户gRPC服务器发出gRPC请求.

So I keep getting this 'unexpected token u in JSON at position 0' error. I'm currently make a request from the main initiator which is making a gRPC request to customers gRPC server.

当我不对文件进行容器化并在每个目录中手动npm安装软件包时,它可以正常运行.但是,由于某些原因,当我对文件进行容器化时,会出现此问题.

When I don't containerize my files and manually npm install packages in each directory, it works smoothly. However, for some reason when I containerize my files, it has this issue.

通常,异步请求会发生此问题(gRPC是异步的,因此很有意义),我认为它们正在争先恐后地完成工作,但从来没有这样做.但是dockerFile实际上是在做我正在手动做的事情(有效...)

Usually this issue occurs with asynchronous requests (gRPC is async so makes sense), and I think they're racing to completion, but don't ever get to do so. But the dockerFile is literally doing what I'm doing manually (which works...)

我只是迷失了为什么会这样.

I'm just currently lost as to why this is the case.

错误

Error:
undefined:1
undefined
^

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at horus.grabTrace (/usr/src/app/horus/horus.js:52:23)
    at ClientUnaryCall.<anonymous> (/usr/src/app/main.js:119:8)
    at ClientUnaryCall.emit (events.js:210:5)
    at Object.onReceiveMetadata (/usr/src/app/node_modules/grpc/src/client_interceptors.js:1202:15)
    at InterceptingListener._callNext (/usr/src/app/node_modules/grpc/src/client_interceptors.js:568:42)
    at InterceptingListener.onReceiveMetadata (/usr/src/app/node_modules/grpc/src/client_interceptors.js:582:8)
    at callback (/usr/src/app/node_modules/grpc/src/client_interceptors.js:845:24)

文件结构

**3 Different Services**

**Books**
-stubs
  -booksStub
-BooksServer.js

**Customers**
-stubs (2 stubs for intraservice request)
  -booksStub
  -customersStub 
-customersServer.js
-Dockerfile

**Main**
-Main Initiator
-Dockerfile

Docker文件(全部)

**Dockerfile (Customers Service)**
FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
RUN npm install nodemon -g
EXPOSE 6000
CMD ["nodemon", "customersServer.js"]

**Dockerfile (Books Service)**
FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
RUN npm install nodemon -g
EXPOSE 30043
CMD ["nodemon", "booksServer.js"]

**Dockerfile (Main Service)**
FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
EXPOSE 4555
CMD ["node", "main.js"]

推荐答案

此JSON解析错误是根本问题的征兆,该问题是某些代码期望JSON字符串并得到 undefined .问题中的信息不足以确定确切的原因,但是堆栈跟踪在 JSON.parse 调用和gRPC库中的事件之间有两个条目:

This JSON parsing error is a symptom of the root problem, which is that some piece of code is expecting a JSON string and is getting undefined. The information in the question isn't enough to determine exactly why this is happening, but the stack trace has two entries between the JSON.parse call, and an event from the gRPC library:

    at JSON.parse (<anonymous>)
>   at horus.grabTrace (/usr/src/app/horus/horus.js:52:23)
>   at ClientUnaryCall.<anonymous> (/usr/src/app/main.js:119:8)
    at ClientUnaryCall.emit (events.js:210:5)

这表明在第52行上, horus.js 正在调用 JSON.parse ,并且错误表明它正在传递 undefined 未定义" 而不是JSON字符串.该行又位于 horus.grabTrace 函数中,该函数从 main.js 的第119行的匿名事件处理程序中调用.因此,此事件处理程序正在将无效数据传递给 horus.grabTrace ,或者正在传递有效数据而 horus.grabTrace 处理不正确.反过来,这可能是由于事件处理程序收到意外值所致.

This shows that on line 52, horus.js is calling JSON.parse and the error indicates that it's passing undefined or "undefined" instead of a JSON string. This line is in turn in the horus.grabTrace function, which is being called from an anonymous event handler on line 119 of main.js. So, either this event handler is passing invalid data to horus.grabTrace, or it is passing valid data and horus.grabTrace is handling it incorrectly. This may in turn be caused by the event handler receiving an unexpected value.

一种简单的调试方法是在事件处理程序中对 horus.grabTrace 的调用周围添加一个 try ... catch 块.错误注销传递给事件处理程序的值,并将该值传递给 horus.grabTrace ,然后重新抛出该错误以停止执行.这将告诉您代码未正确处理哪个值,这应该有助于您了解要更改的内容.

A simple way to debug this is to add a try...catch block around the call to horus.grabTrace in the event handler, and when there is an error log out the value passed to the event handler, and the value passed to horus.grabTrace, and then re-throw the error to stop execution. This will tell you which value your code is not handling correctly, which should help you understand what to change.

这篇关于gRPC节点错误:JSON在位置0处出现意外令牌u的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆