I'm having a strange issue that when I try to create a client to connect to my CosmosDB, its throwing in an invalid URL error.
The code is simple and I know the keys and connection string are correct because in a different project I'm using them to connect with the DB in an Azure functions app (but that's in C#/.Net). But for various reasons I need to create this in node. I have tried creating the client with just the connection string and with the endpoint and key pass separately, both give the same result.
Here's my code -
const { CosmosClient } = require("@azure/cosmos");
const endPoint = "<obscured endpoint>";
const url = new URL(endPoint);
console.log(url.toJSON()); //this is here just to check the url since I was getting the error, and works fine
const key = "<obscured key>";
const connectionString = "<obscured connection string that encodes the endpoint and key>";
const cosmosClient = new CosmosClient({ connectionString });
This is error:
Uncaught TypeError TypeError [ERR_INVALID_URL]: Invalid URL
at __node_internal_captureLargerStackTrace (internal/errors:491:5)
at NodeError (internal/errors:400:5)
at onParseError (internal/url:565:9)
at URL (internal/url:645:5)
at checkURL (c:\Web3Scan\Web3Scan\node_modules\@azure\cosmos\src\utils\checkURL.ts:5:10)
at CosmosClient (c:\Web3Scan\Web3Scan\node_modules\@azure\cosmos\src\CosmosClient.ts:68:22)
at <anonymous> (c:\Web3Scan\Web3Scan\app.js:16:23)
at Module._compile (internal/modules/cjs/loader:1218:14)
at Module._extensions..js (internal/modules/cjs/loader:1272:10)
at Module.load (internal/modules/cjs/loader:1081:32)
at Module._load (internal/modules/cjs/loader:922:12)
at executeUserEntryPoint (internal/modules/run_main:82:12)
at <anonymous> (internal/main/run_main_module:23:47)
When I look at checkURL.ts it looks like its doing the same thing as my test, so I'm stumped -
export function checkURL(testString: string): URL {
return new URL(testString); //same exact check i'm doing that's successful
}
I believe the reason your code is failing is because the connectionString
parameter to CosmosClient
is of type string here
, however you are passing it as an object.
Please try by changing the following line of code:
const cosmosClient = new CosmosClient({ connectionString });
to
const cosmosClient = new CosmosClient(connectionString);
And you should not get any errors.