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"}'
}