Netlify remote no access to Mongo Db

I get the following error when invoking
curl https://tik2.netlify.app/.netlify/functions/mongo\?coll=index:

{"errorType":"SyntaxError","errorMessage":"Unexpected end of JSON input","trace":["SyntaxError: Unexpected end of JSON input"," at JSON.parse (<anonymous>)"," at Runtime.handler (/var/task/functions/mongo.js:26765:21)"," at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1028:29)"]}

However, curl http://localhost/.netlify/functions/mongo\?coll\=index returns ok: [{"name":"convert_feet_cm","title":"converter","text":"convert feet to cm","cat":"......
Here is the function
Any help?

Hi @tik9

In the function I see

function main() { return new MongoClient(process.env.mongo!).connect() }

Have you configured the environment variable in the Netlify UI? Have you also tried logging the response status/text from mongo?

If you put the call to mongo inside a try...catch or check the status code returned, you have a better chance of handling unpredicted errors

Have you also tried logging the response status/text from mongo
No idea how to do this - I use Mongo Atlas and cannot see any logging.

I get the same error on https://tik2.netlify.app for the mongo.ts file with try..catch, see the dev console network tab → mongo function:
{ "errorType": "SyntaxError", "errorMessage": "Unexpected end of JSON input", "trace": [ "SyntaxError: Unexpected end of JSON input", " at JSON.parse (<anonymous>)", " at Runtime.handler (/var/task/functions/mongo.js:26761:21)", " at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1028:29)" ] }

Local with ntl dev works, so it should be a problem with ntl?

Did you add try...catch and retry?

2 Likes

@hrishikesh see my edit 4d ago.

:wave: @tik9

Could you also confirm that you’ve configured the environment variable in the Netlify UI as @coelmay mentioned? We’re asking because if env vars haven’t been configured in Netlify but you have it stored locally, this could result in errors during runtime, but not with ntl dev.

I can confirm to store the env vars only in ntl: ntl env:list; 10 environment variables for site tik2. I do not use other vars than the ones from ntl.
I get Netlify internal ID: 01GFKM6X6TP9782838T5R6XNQ2 as Connection details for t–tik2.netlify.app. Does it help?

Howdy! I am not a javascript expert, but I do not know and google couldn’t tell me, what the difference between:

process.env.mongo! and process.env.mongo is? I’d never seen the one with the ! before and your variable certainly does not have a name with a ! in it since that is not a valid character in environment variables.

If that isn’t the problem, I think you will have to figure out how to show more details about when you connect if you want our help. I understand you don’t know how to do it with mongo atlas - but neither do we. So I suppose you could ask the author for help in turning on a debug mode or something?

It now works, i had the wrong code logic checking if event.body is emptfy in Nodejs. Thanks all.

1 Like

thanks so much for coming back and letting us know! Glad everything is working now. Happy building :rocket: