[SalesForce] Cannot connect to Salesforce Org from VS Code/Salesforce CLI: Must pass a username and/or OAuth options when creating an AuthInfo instance

I'm trying to connect to my Trailhead playground org with the Salesforce CLI on VS Code. Here's what I've done:

Created the project then tried authorizing a devhub

I created the project then clicked on Authorizing a dev hub. I was prompted for a login – which I successfully authenticated then the browser window redirected to <salesforce org>/lightning/setup/SetupOneHome/home.

enter image description here

However, when I try to Run a SOQL query I get the following:

enter image description here

enter image description here

I suspect it's an issue with my computer setup, because when I tried creating a separate project entirely I got the same error. Following this didn't help me either.

Can anyone point me in the right direction so I know how to properly authenticate?

Best Answer

You need to set the org with defaultusername, not defaultdevhubusername (the latter is used for creating packages and scratch orgs, the former is for SOQL, retrieve, deploy, etc).

To set the value, press Ctrl+Shift+p, then choose "SFDX: Set a Default Org" (you can type in "default org" to quickly find the command), then select your trailhead username. This will set the default user name and allow the other commands to work. Alternatively, click on the Terminal (Ctrl+Shift+~), then type in:

sfdx force:config:set defaultusername=mytrailhead@whatever.com

In the future, make sure you use "SFDX: Authorize an Org" if you want to specify a development org, and "SFDX: Authorize a Dev Hub" for setting a Dev Hub org.