Can not load file

When trying to load the validateLab1.js file I get the following error

> load('validateLab1.js');
2019-07-20T13:21:47.165+0200 E QUERY    [thread1] SyntaxError: missing ; before statement :
@(shell):1:1

2019-07-20T13:21:47.165+0200 E QUERY    [thread1] Error: error loading js file: validateLab1.js :
@(shell):1:1

MongoDB shell version: 3.2.21

semicolon not needed at the end of load command

It stills doesn’t work.
So I am going with the console to the folder where the file is. Running “mongo” with the connection given by the course and then running load() command. I already have the solution of the first problem but can not verifiy it because I can not load the file. Am I missing something?

@Adrian_18948

What OS are you using? Basically, what you describe works for me in both MacOS and Linux – I don’t have a Windows system at the moment to test unfortunately.

Here’s a sequence of commands that works in Linux
cd <directory where script is saved>
ls
(returns)
validateLab1.js

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
(this takes you into the Atlas cluster – now you should see the shell prompt)
MongoDB Enterprise Cluster0-shard-0:PRIMARY>
(and you enter)
MongoDB Enterprise Cluster0-shard-0:PRIMARY>load(‘validateLab1.js’)
(should return)
true

Also note that whether you have a semicolon at the end of the load() statement or not won’t affect the command. FWIW.

I am using windows 8.1

I tried with cmd and with git bash (with this one the mongo shell is bugged, for example is you write load(‘validateLab1.js’) it throw the same error and after that it does not response to any command for example writing listFiles() or trying to access a variable does not work)

I also tried using the full path like commented here here. load(“C:\Users\adrian\Code\Tutorials\MongoDB University\m121_aggregation\validateLab1.js”) in CMD. But I get the same error: SyntaxError: missing ; before statement :

It may be due to the quotes

I did a simple test

load(“C:\Users\adrian\Code\Tutorials\MongoDB University\m121_aggregation\validateLab1.js”)
2019-07-25T08:30:56.009+0530 E QUERY [js] SyntaxError: illegal character @(shell):1:5

load(“C:\Users\adrian\Code\Tutorials\MongoDB University\m121_aggregation\validateLab1.js”)
2019-07-25T08:31:22.869+0530 E - [js] file [C:UsersadrianCodeTutorialsMongoDB Universitym121_aggregationalidateLab1.js] doesn’t exist
2019-07-25T08:31:22.877+0530 E QUERY [js] Error: error loading js file: C:UsersadrianCodeTutorialsMongoDB Universitym121_aggregationalidateLab1.js :
@(shell):1:1
See the difference in two commands
In first case i just cut & pasted your command.It failed due to inverted commas
In second case i typed the quotes.No syntax error but it failed as i gave non existent path

I am still getting SyntaxError: missing ; before statement. Error: error loading js file

Can you paste your screen shots step by step how you loaded or logs from your terminal
How did you connect to mongo?

If you need something more tell me. Adding “;” at the end does not fix it. Thanks in advance




Using complete path

Fixed by updating MongoDB. It was a bug of the version indeed. Thanks to everyone for your time.

1 Like

Good!!
Thanks for sharing the fix

Works with double slash and absolute path on Windows 10
load(“C:\validateLab1.js”)

Probably because how the Mongo implementation handles path string in raw format feeding it to C++ (my speculation)