Liam_Doyle
(Liam Doyle)
1
Hi.
I am currently trying to use MongoDB for VS Code to run CRUD commands that I would usually run in Robo 3T but they are not working - example script that works as intended in Robo 3T:
> var email = '<user email>';
> var user = db.getCollection('<collection name 1>').find({email: email});
>
> db.getCollection('<collection name 1>').deleteMany({platformUserId: user[0]._id})
> db.getCollection('<collection name 2>').deleteMany({email: email})
> db.getCollection('<collection name 3>').deleteMany({email: email})
When I try to run this in a playground I get the error ‘Cannot read property ‘_id’ of undefined’
Any ideas?
Thanks,
Liam
steevej
(Steeve Juneau)
2
In the code you share the only place we see _id access is in
Since you use user[0] then it must be that user[0] really does not exist. You code is not safe because you use the result of db…find() without testing for a result or not. Many reasons might explain why you do not get user[0].
- You are not connected to the appropriate server
- You are not using the appropriate database
- You are not calling getCollection() with the appropriate collection name
- There is no user that matches { “email” : email }
- You might need to await when you initialize user with “db…find(…)”
1 Like