Diary For Week 10 - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki

Back to dev diaries

2024/12/3

Today in class, I made a profile.html page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">

        const jsonData = [
        {
            "id": 12345,
            "createdAt": "2024-03-05T17:56:43.302Z",
            "date": "2023-05-10T17:50:40:.202Z",
            "steps": 11968,
            "distanceMiles": 6.3,
            "flights":  0,
            "activeEnergyCals": 640,
            "handwashingSeconds": 15,
            "restingEnergyCals": 1683,
            "soundLevel": 79,
        },

        {
            "id": 67891,
            "createdAt": "2024-03-05T17:55:42.202Z",
            "date": "2023-06-10T17:50:40:.202Z",
            "steps": 8129,
            "distanceMiles": 3.8,
            "flights":  2,
            "activeEnergyCals": 385,
            "handwashingSeconds": 11,
            "restingEnergyCals": 1599,
            "soundLevel": 75
        }
    ];

    const bodyTag = document.getElementsByTagName("body")[0];
    for (let i in jsonData){
        const watchData = document.createElement("div");
        watchData.classList = ["watch-data"];

        const w = document.createElement("header");
        w.textContent = jsonData[i].name;

        watchData.appendChild(w);
        bodyTag.appendChild(watchData);

    }

    </script>
    
</body>
</html>

Afternoon Office Hours

I continued to work on the error from a couple days ago. The error was due to to this part of my code:

const jsonBody = {};
        jsonBody["date"] = dataPoint[0];
        jsonBody["steps"] = dataPoint[1];
        jsonBody["distance"] = dataPoint[2];
        jsonBody["flights"] = dataPoint[3];
        jsonBody["activeEnergyCals"] = dataPoint[4];
        jsonBody["handwashingSeconds"] = dataPoint[5];
        jsonBody["restingEnergyCals"] = dataPoint[6];
        jsonBody["soundLevel"] = dataPoint[7];
        jsonBody["userId"] = id;

Before I made the fix, the line jsonBody["date"] = dataPoint[0];was not there causing it to say undefined.

The New Error

Once fixing the last issue, I ran convert.js again and got a new error:

const newData = await prisma.dailyWatchData.create({
       data: {
         date: "2023-11-06T00:00:00.000-08:00",
         steps: "5533",
                ~~~~~~
         distanceMiles: undefined,
         flights: "1",
         activeEnergyCals: "262",
         handwashingSeconds: "15",
         restingEnergyCals: "1671",
         soundLevel: "69",
         userId: 8
       }
     })

Argument `steps`: Invalid value provided. Expected Int, provided String.
    at Cn (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:116:5888)
    at _n.handleRequestError (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:6510)
    at _n.handleAndLogRequestError (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:6188)
    at _n.request (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:5896)
    at async l (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:128:10871)
    at async exports.createDailyWatchData (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/dailyWatchData.js:11:21) {
  clientVersion: '5.10.2'
}
POST /daily-watch-data 500 0.896 ms - 35

This error was due to steps printing as a string instead of an Int, the fix:

const newData = await prisma.dailyWatchData.create({
      data: {
        date: formattedDate,
        steps: Number(req.body.steps),
        distanceMiles: Number(req.body.distanceMiles),
        flights: Number(req.body.flights),
        activeEnergyCals: Number(req.body.activeEnergyCals),
        handwashingSeconds: Number(req.body.handwashingSeconds),
        restingEnergyCals: Number(req.body.restingEnergyCals),
        soundLevel: Number(req.body.soundLevel),
        user:{
          "connect": {
            "id":  Number(req.body.userId),
          }
        } 
      },

The Next Error

After fixing the last error, steps no longer displays a string (yay), now this is the new error:

Argument `user` is missing.
    at Cn (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:116:5888)
    at _n.handleRequestError (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:6510)
    at _n.handleAndLogRequestError (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:6188)
    at _n.request (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:123:5896)
    at async l (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/@prisma/client/runtime/library.js:128:10871)
    at async exports.createDailyWatchData (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/dailyWatchData.js:11:21) {
  clientVersion: '5.10.2'
}
POST /daily-watch-data 500 0.878 ms - 35

Here is the fix:

const newData = await prisma.dailyWatchData.create({
      data: {
        date: formattedDate,
        steps: Number(req.body.steps),
        distanceMiles: Number(req.body.distanceMiles),
        flights: Number(req.body.flights),
        activeEnergyCals: Number(req.body.activeEnergyCals),
        handwashingSeconds: Number(req.body.handwashingSeconds),
        restingEnergyCals: Number(req.body.restingEnergyCals),
        soundLevel: Number(req.body.soundLevel),
        user:{
          "connect": {
            "id":  Number(req.body.userId),
          }
        } 
      },
    });

This line:

user:{
          "connect": {
            "id":  Number(req.body.userId),
          }

was the change/fix.

2024/13/3

Yesterday, I got my convert.js script to run with no errors! This script took my csv file with apple watch data and added it to my prisma studio for a test user 'abc'. Here is the code:

//../../../../dsa-23au/java-dsa/Torsten-David.app/src/main/resources/AppleWatchData_myData.csv 

const fs = require('fs');
const jwt = require('jsonwebtoken');
const file = fs.readFileSync("../../../../dsa-23au/java-dsa/Torsten-David.app/src/main/resources/AppleWatchData_myData.csv")

const lines = file.toString().split("\n")

const token = fs.readFileSync("token.txt").toString().trim();
console.log(`Token ${token}`);

const { id } = jwt.decode(token, {json: true });

const main = async() => {
    console.log(lines.length);
    for (let i in lines){
        const dataPoint = lines[i].split(',');
        console.log(`Data ${ i } of ${ lines.length }`);
        console.log(JSON.stringify(dataPoint));

        if(i === 0 ){
            continue;
        }

        const jsonBody = {};
        jsonBody["date"] = dataPoint[0];
        jsonBody["steps"] = dataPoint[1];
        jsonBody["distance"] = dataPoint[2];
        jsonBody["flights"] = dataPoint[3];
        jsonBody["activeEnergyCals"] = dataPoint[4];
        jsonBody["handwashingSeconds"] = dataPoint[5];
        jsonBody["restingEnergyCals"] = dataPoint[6];
        jsonBody["soundLevel"] = dataPoint[7];
        jsonBody["userId"] = id;


        const response = await fetch(
            "http://localhost:5000/daily-watch-data",
            {
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "Authorization": `Bearer ${token}`,
                },
                "body": JSON.stringify(jsonBody)
            }
        );
        const json = await response.json();

        console.log(JSON.stringify(json));

         
    }


}

main().then(() => console.log("Done"));

I mean look at this beautiful result with no error message:

0/5/2023
POST /daily-watch-data 200 786.699 ms - 225
10/6/2023
POST /daily-watch-data 200 297.582 ms - 224
10/7/2023
POST /daily-watch-data 200 298.724 ms - 224
10/8/2023
POST /daily-watch-data 200 299.399 ms - 224
10/9/2023
POST /daily-watch-data 200 303.319 ms - 225
10/10/2023
POST /daily-watch-data 200 299.688 ms - 224
10/11/2023
POST /daily-watch-data 200 298.575 ms - 225
10/12/2023
POST /daily-watch-data 200 296.677 ms - 226
10/13/2023
POST /daily-watch-data 200 298.075 ms - 225
10/14/2023
POST /daily-watch-data 200 306.935 ms - 225
10/15/2023
POST /daily-watch-data 200 294.240 ms - 225
10/16/2023
POST /daily-watch-data 200 297.732 ms - 225
10/17/2023
POST /daily-watch-data 200 297.820 ms - 226
10/18/2023
POST /daily-watch-data 200 294.648 ms - 225
10/19/2023
POST /daily-watch-data 200 300.138 ms - 226
10/21/2023
POST /daily-watch-data 200 298.746 ms - 226
10/22/2023
POST /daily-watch-data 200 297.955 ms - 225
10/23/2023
POST /daily-watch-data 200 296.033 ms - 225
10/24/2023
POST /daily-watch-data 200 302.493 ms - 225
10/25/2023
POST /daily-watch-data 200 366.852 ms - 225
10/26/2023
POST /daily-watch-data 200 455.074 ms - 226
10/27/2023
POST /daily-watch-data 200 987.881 ms - 225
10/28/2023
POST /daily-watch-data 200 864.591 ms - 225
10/29/2023
POST /daily-watch-data 200 860.922 ms - 225
10/30/2023
POST /daily-watch-data 200 296.835 ms - 225
10/31/2023
POST /daily-watch-data 200 295.962 ms - 226
11/1/2023
POST /daily-watch-data 200 295.771 ms - 225
11/2/2023
POST /daily-watch-data 200 297.902 ms - 225
11/4/2023
POST /daily-watch-data 200 299.917 ms - 226
11/5/2023
POST /daily-watch-data 200 296.279 ms - 225
11/6/2023
POST /daily-watch-data 200 297.663 ms - 225

While this runs with no errors and posts the watch data to my prisma studio, for some reason, all the distances say 0.

⚠️ **GitHub.com Fallback** ⚠️