skip to Main Content

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:

Current File Structure

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:

Error From Building

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


  1. Try this:

    exports.getTournamentData = (tourneySlug) => {
    // your code
    }
    
    Login or Signup to reply.
  2. You are exporting the single function getTournamentData from startgg.service.js. This means you need to use Destructuring assignment when you import the function.

    In addition, as per Node docs:

    A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. ‘./startup/index.js’) must also be fully specified.

    You should implement:

    Server.js

    import { getTournamentData } from './third-party/startgg-api-service/startgg.service.js';
    import express from 'express';
    const app = express();
    //...
    

    startgg.service.js

    import { startggVariables } from './startgg.variables.js';
    import axios from 'axios';
    //...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search