ReplitDB Syntax error unable to throw error

Question:
I some how errored so hard I couldn’t even throw an error. I am writing a bookie interface where people can look up odds for upcoming games. For now, only for the NBA. I have logged a value in dbfns.js It is an object that contains the information of odds for upcoming games. I have seen this output on the console, so I am pretty sure it isn’t impossible to store and recall. The problem must be in the-odds-api.js resquest.odds() method.

index.js code that throws the error

import Database from '@replit/database';
const db = new Database();

db.list().then(keys => {
  console.log(keys);
});
db.get('nba').then(value => {
  console.log(value);
});


Output

[ 'nba' ]
/home/runner/jsbookie-backend/node_modules/@replit/database/index.js:36
          throw new SyntaxError(
                ^

SyntaxError: Failed to parse value of nba, try passing a raw option to get the raw value
    at /home/runner/jsbookie-backend/node_modules/@replit/database/index.js:36:17
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.12.1

index.js

import {dbfn} from './dbfunctions.js';
import {request, sport} from './the-odds-api.js';

request.odds(sport.nba);

the-odds-api.js

import fetch from 'node-fetch';
import {dbfn} from './dbfunctions.js'



const api = {
  key : process.env['api_key'],
  base_url : 'https://api.the-odds-api.com',
  regions : 'us'
};

const sport = {
  
  nba: { // This is what gets passed in request.odds(desiredSport)
    dbkey: 'nba',
    key: 'basketball_nba',
    group: 'Basketball',
    title: 'NBA',
    description: 'US Basketball',
    active: false,
    has_outrights: false
  }
};

const request = {
  odds: (desiredSport) => {
    console.log(`${desiredSport.dbkey} :contains:`, desiredSport);
    let url = ':base_url/v4/sports/:sport_key/odds/?apiKey=:api_key&regions=:regions&markets=h2h,spreads';
    url = url.replace(':base_url', `${api.base_url}`)
    url = url.replace(':sport_key',`${desiredSport.key}`);
    url = url.replace(':api_key', `${api.key}`);
    url = url.replace(':regions', `${api.regions}`)
    let header = new Headers({ 'sport': '${desiredSport.stringify()}' });
    

    fetch(url, header) // Error: TypeError: Request with GET/HEAD method cannot have body at new Request
/*
This is where I think the problem originates. I am trying to somehow pass the sport.nba object so that I can store it because attempting to refer to it outside the promise puts it out of scope. This is something I decided to try to but I am sure there is a right way to do this.
/*
      .then((response, request) => {
        return [
          request.header.sport.json(),
          response.json()
        ];
      })
      .then((array_sport_response) => {
        dbfn.set(`${array_sport_response[0].dbkey}`, array_sport_response[1]);
      })
      .catch((error) => {
        console.error('Error:', error);
      })
  }
};

dbfns.js.

import Database from '@replit/database';
// declare the replit database as db
const db = new Database();

const dbfn = {
  
log: (dbkeyStr) => {
    console.log(`${dbkeyStr} contains: `);
    db.get(`${dbkeyStr}`)
      .then((value) => {
        console.log(value.stringify());
      });
  }

I found the offending line as soon as I went back to my code.

Here it is:
the-odds-api.js

let header = new Headers({ 'sport': '${desiredSport.stringify()}' });

However, I’m not sure why. Perhaps the fact that I passed $ as part of the string?

Still, it IS a string either way. Have I uncovered a bug?

You’re supposed to use backticks, not quotes if you want to use ${}

Yes, I discovered that. Does the '$' prevent it from being a string?

If you’re using quotes, it will be a string. If you’re using backticks, it will still be a string, but the value will be of the variable inside ${}

1 Like

Given there’s no other text, I don’t know why you wouldn’t just do desiredSport.stringify() rather than using template literals…

1 Like

The second parameter in fetch is supposed to be options, it looks like you’re trying to use a header instead of options. I think you should be doing something more like this:

fetch(url, {
	method: "GET",
	headers: {
		sport: desiredSport.stringify()
	}
});
1 Like

Thanks @MattDESTROYER ,

Thanks for the point in the right direction. It doesn’t work for reasons that are unclear to me. When I get the response back there is no sport property in either the request nor response. I suspect this may be due to how the api I query is written.

The solution appears to be much simpler. I save the dbkey as a property of the request.method that calls fetch.

the-odds-api.js

const request = {
  odds: (desiredSport, mode) => {

    let url;
    if (mode.debug) {
        url = mode.url;
    }
    else {
        url = ':base_url/v4/sports/:sport_key/odds/?apiKey=:api_key&regions=:regions&markets=h2h,spreads&oddsFormat=decimal';
        url = url.replace(':base_url', `${api.base_url}`);
        url = url.replace(':sport_key', `${desiredSport.key}`);
        url = url.replace(':api_key', `${api.key}`);
        url = url.replace(':regions', `${api.regions}`);
    }

    
    request.dbkey = desiredSport.dbkey;
  },
  dbkey = ''
}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.