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®ions=: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());
});
}