Netlify Functions errorMessage: 'lambdaFunc[lambdaHandler] is not a function'

I’m trying to move a function from Google Cloud to Netlify. My code is

var fetch = require("node-fetch");
var SpotifyWebApi = require("spotify-web-api-node");

function getApiUrl(id) {
	return `https://api.spotify.com/v1/playlists/${id}/tracks?market=US`;
}
exports.handler = async function (event, context) {
	event.set("Access-Control-Allow-Origin", "*");
	let id = event.queryStringParameters.id;
	console.log(id);

	var spotifyApi = new SpotifyWebApi({
		clientId: process.env.ClientID,
		clientSecret: process.env.ClientSecret,
	});
spotifyApi
	.clientCredentialsGrant()
	.then(
		function (data) {
			// Save the access token so that it's used in future calls
			spotifyApi.setAccessToken(data.body["access_token"]);
		},
		function (err) {
			console.log(
				"Something went wrong when retrieving an access token",
				err.message,
			);
		},
	)
	.then(() => {
		const headers = {
			Accept: "application/json",
			"Content-Type": "application/json",
			Authorization: `Bearer ${spotifyApi.getAccessToken()}`,
		};

		fetch(getApiUrl(id), { headers: headers })
			.then((response) => response.json())
			.then((jsonData) => {
				return {
					statusCode: 200,
					body: jsonData,
				};
			});
	});

};

When I try to run it with the command "netlify functions:invoke getSpotifyData --querystring “id=1” ", I get the following error.

Error during invocation: {
  errorMessage: 'lambdaFunc[lambdaHandler] is not a function',
  errorType: 'TypeError',
  stackTrace: [
    'Object._executeSync (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lambdalocal.js:286:47)',
    'Object.execute (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lambdalocal.js:83:22)',
    'C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\src\\utils\\serve-functions.js:170:24',
    'Layer.handle [as handle_request] (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\layer.js:95:5)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:137:13)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:131:14)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:131:14)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:131:14)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:131:14)',
    'next (C:\\Users\\Emily\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\express\\lib\\router\\route.js:131:14)'
  ],
  level: 'error',
  [Symbol(level)]: 'error',
  [Symbol(message)]: '{"errorMessage":"lambdaFunc[lambdaHandler] is not a function","errorType":"TypeError","stackTrace":["Object._executeSync (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\lambda-local\\\\build\\\\lambdalocal.js:286:47)","Object.execute (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\lambda-local\\\\build\\\\lambdalocal.js:83:22)","C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\src\\\\utils\\\\serve-functions.js:170:24","Layer.handle [as handle_request] (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\layer.js:95:5)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:137:13)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:131:14)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:131:14)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:131:14)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:131:14)","next (C:\\\\Users\\\\Emily\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\netlify-cli\\\\node_modules\\\\express\\\\lib\\\\router\\\\route.js:131:14)"],"level":"error"}'
}

I commented out my code and replaced it with the following straight from the Netlify docs and I’m still getting the same error.

exports.handler = async function (event, context) {
return {
	statusCode: 200,
	body: JSON.stringify({ message: "Hello World" }),
    };
};

Any idea what I’m doing wrong?

Solved it. The problems were:

a. I was in the wrong directory.
b. This line: event.set(“Access-Control-Allow-Origin”, “*”);

1 Like

Glad you found the solution! Thanks for coming back to report for others :slight_smile:

1 Like