I'm trying to implement geocoding to my express app and I decided to use MapBox client API. However when I tried to configure it, it's still throwing following error. Please help me
Error: Cannot create a client without an access token
at NodeClient.MapiClient (D:\NODE COURSE\Colt Steele\DerpCamp\node_modules\@mapbox\mapbox-sdk\lib\classes\mapi-client.js:25:11)
at new NodeClient (D:\NODE COURSE\Colt Steele\DerpCamp\node_modules\@mapbox\mapbox-sdk\lib\node\node-client.js:7:14)
at createNodeClient (D:\NODE COURSE\Colt Steele\DerpCamp\node_modules\@mapbox\mapbox-sdk\lib\node\node-client.js:24:10)
at D:\NODE COURSE\Colt Steele\DerpCamp\node_modules\@mapbox\mapbox-sdk\services\service-helpers\create-service-factory.js:13:16
at Object.<anonymous> (D:\NODE COURSE\Colt Steele\DerpCamp\src\controllers\CampgroundController.js:6:18)
Here is my configuration
const Geocoding = require('@mapbox/mapbox-sdk/services/geocoding')
const map_token = process.env.MAPBOX_TOKEN
const geocoder = Geocoding({accessToken: map_token})
VERY IMPORTANT NOTE
There is no space between MAPBOX_TOKEN and my actual api key in my dev.env file.
CodePudding user response:
If you're running the application locally, process.env will return undefined. This issue can be resolved using the dotenv package.
Installation:
npm install dotenv --save
Example usage:
const dotenv = require("dotenv"); // Define the dotenv package
dotenv.config(); // Call the config function
The config function only needs to be called in the main file (commonly called index.js) and will allow process.env to work throughout the project.
