0

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"]
TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15

1 Answers1

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.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • Yes, the error is definitely located there. I found the error. Basically we are sending back metadata from the server to client. This logic works when I don't containerize all my services, but when I do containerize it, the metadata is undefined... I believe that something with Docker networking is causing the metadata to be undefined. gRPC has been a *fun* time so far :/. – TheRoadLessTaken Jul 31 '20 at 21:35
  • For some reason when I dockerize the books service, the booksStub (client), cannot communicate with the server and hence the metadata the gRPC Books server is supposed to receive is undefined. – TheRoadLessTaken Aug 01 '20 at 15:20
  • If the client fails to communicate with the server, it should get an empty `Metadata` object in the `metadata` event. This question seems to indicate that you are not handling that correctly, and you should fix that. The connectivity problem is a separate issue and should be handled in a separate question. – murgatroid99 Aug 01 '20 at 16:31