I trying to set up a Node.js backend for an Angular app I am creating.
I got the server set up, and am now trying to call a function from a different class that will hit a third-party API.
My file structure looks as follows:
My server.js
:
import getTournamentData from './third-party/startgg-api-service/startgg.service'; // ERROR HAPPENING HERE
import express from 'express';
const app = express();
app.get('/', (req, res) => {
//getTournamentData('tempest-96')
res.send('Hello World');
});
app.listen(3000, () => {
console.log('Server listening on Port 3000');
});
My startgg.service
:
import { startggVariables } from './startgg.variables';
import axios from 'axios';
export function getTournamentData(tourneySlug) {
let body = getTournamentDataQuery(tourneySlug);
axios.post(startggVariables.API_KEY, body)
.then(res => {
console.log(res.data)
})
}
function getTournamentDataQuery(tourneySlug) {
let body = {
"query": `
query TournamentQuery($slug: String) {
tournament(slug: $slug){
id
name
events {
id
name
}
}
}
`,
"operationName": "TournamentQuery",
"variables": {"slug": tourneySlug}
}
return body;
}
My startgg.variables
:
export var startggVariables = {
API_KEY: 'https://api.start.gg/gql/alpha'
}
I am getting the following error when I try to run the server:
I have tried the relative route from the error message, I have tried importing * from that file, and I have tried export default instead of just export, but I get the same error with all.
2
Answers
Try this:
You are exporting the single function
getTournamentData
fromstartgg.service.js
. This means you need to use Destructuring assignment when you import the function.In addition, as per Node docs:
You should implement:
Server.js
startgg.service.js