Home » Javascript » Update database with dynamic object names (Node.js)

Update database with dynamic object names (Node.js)

Posted by: admin August 19, 2018 Leave a comment


I’m scraping some web data and adding it to my database. The problem is I would like the structure to result in a collection of objects with dynamically generated names.

In the below example I would like var URLString to dynamically become the name of the object at the bottom. Right now, every object that is added to my database has the name ‘URLString’, not the value of the URLString variable.

I’ve looked around but cannot figure out if this is possible (much less good or bad practice) but it seems like it should be rudimentary.

I hope someone can help. Thanks

$('.live-update').each(function(i, element){

        var homeTeamAbbr = $(this).find('tbody').children('tr').eq(0).find('a').html().split("alt/").pop().split('.svg')[0]
        var awayTeamAbbr = $(this).find('tbody').children('tr').eq(1).find('a').html().split("alt/").pop().split('.svg')[0]
        var homeTeam = $(this).find('tbody').children('tr').eq(0).find('a.team').text().trim();
        var awayTeam = $(this).find('tbody').children('tr').eq(1).find('a.team').text().trim();
        var gameTime = $(this).find('.pregame-date').text().trim()

        console.log(homeTeam + " @ " + awayTeam + ": " + gameTime)
        console.log(homeTeamAbbr + " @ " + awayTeamAbbr)

        var urlDate = moment().format('YYYYMMDD');

        var URLString = "NBA_" + urlDate + "_" + awayTeamAbbr + "@" + homeTeamAbbr;
        var docRef = database.collection('Schedule').doc(urlDate).collection(URLString);

        var boxScoreURL = "www.cbssports.com/nba/gametracker/boxscore/" + URLString

        var objectNamed = URLString

        var setAda = docRef.add({URLString: {
          homeTeam: homeTeam,
          awayTeam: awayTeam,
          date: gameTime,
          homeTeamAbbr: homeTeamAbbr,
          awayTeamAbbr: awayTeamAbbr,
          boxScoreURL: boxScoreURL