gRPC-Node Error: Unexpected token u in JSON at position 0

蓝咒 提交于 2021-02-11 14:16:59

问题


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.

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.

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

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)

File Structure

**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 Files (All)

**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"]

回答1:


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)

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.

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.



来源:https://stackoverflow.com/questions/63184625/grpc-node-error-unexpected-token-u-in-json-at-position-0

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