Torsten‐Dev‐Diary - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki
Project Video Backend (what works)
in my api directory, I ran node server.js
, then using thunder client, attempted to create a new user.

Server listening on port 5000
TypeError: Cannot read properties of undefined (reading 'create')
at exports.createNewUser (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/user.js:9:40)
at Layer.handle [as handle_request] (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/.pnpm/[email protected]/node_modules/express/lib/router/layer.js:95:5)
const prisma = require('../db');
const { hashPassword, comparePasswords } = require('../modules/auth');
const { createJWT } = require('../modules/auth');
console.log(JSON.stringify(Object.keys(prisma)));
exports.createNewUser = async function(req, res) {
try {
const user = await prisma.user.create({
data: {
username: req.body.username,
password: await hashPassword(req.body.password)
}
});
(*paul: the prisma reference looks like it's null. I think we are not exporting it from another module correctly. ) once I added the last line of code in my db.js file:
const { PrismaClient } = require('@prisma/client');
const{ parsed } = require('dotenv').config();
console.log(parsed['DATABASE_URL']);
console.log(process.env['DATABASE_URL']);
const prisma = new PrismaClient();
module.exports = prisma;
'''
The create error went away, but now an error about username appeared.
TypeError: Cannot read properties of undefined (reading 'username') at exports.createNewUser (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/user.js:11:36)
This that in the line 'username: req.body.username,' that body is undefined.
Now when I run ssh -o ControlMaster=auto -o ControlPath=/tmp/mysshcontrolpath -fNT -L 5432:localhost:5432 [email protected]
, I get a new error:
PrismaClientValidationError:
Invalid `prisma.user.create()` invocation in
/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/user.js:9:40
This error is due to my schema expecting a passwordHash, but it got a password.
Today during hackathon, Paul gave me a script I can use to upload my csv into my database. You can see the file at WEB-24WI/projects/appleWatchHealthData/api-server/scripts
.
After I added a script, I then wanted to work on a POST for dailyWatchData since I was able to successfully POST a user yesterday. I have created a dailyWatchData.js file located in /handlers:
const prisma = require('../db');
const { parse, format } = require('date-fns');
exports.createDailyWatchData = async (req, res) => {
try {
const parsedDate = parse(req.body.date, 'MM/dd/yyyy', new Date());
const formattedDate = format(parsedDate, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx");
const newData = await prisma.dailyWatchData.create({
data: {
date: formattedDate,
steps: req.body.steps,
distanceMiles: req.body.distanceMiles,
flights: req.body.flights,
activeEnergyCals: req.body.activeEnergyCals,
handwashingSeconds: req.body.handwashingSeconds,
restingEnergyCals: req.body.restingEnergyCals,
soundLevel: req.body.soundLevel,
userId: Number(req.body.userId),
},
});
res.json(newData);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal server error' });
}
};
I also updated router.js:
const express = require('express');
const router = express.Router();
const { createDailyWatchData, updateDailyWatchData, getDailyWatchDataById, deleteDailyWatchData } = require('./handlers/dailyWatchData');
/*
Daily-Watch-Data
*/
router.get('/daily-watch-data', (req, res) => {
// Implementation for GET /daily-watch-data
});
router.get('/daily-watch-data/:id', (req, res) => {
// Implementation for GET /daily-watch-data/:id
});
router.put('/daily-watch-data/:id', (req, res) => {
// Implementation for PUT /daily-watch-data/:id
});
router.post('/daily-watch-data', createDailyWatchData);
router.delete('/daily-watch-data/:id', (req, res) => {
// Implementation for DELETE /daily-watch-data/:id
});
module.exports = router;
Now when I attempt to POST dailyWatchData, I get the following error:
Argument `user` is missing.
(library error messages not in my own files)
at async exports.createDailyWatchData (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/dailyWatchData.js:10:21) {
This error was caused because in this message: const newData = await prisma.dailyWatchData.create({ data: { date: "2023-10-13T00:00:00.000-07:00", steps: undefined, distanceMiles: undefined, flights: undefined, activeEnergyCals: undefined, handwashingSeconds: undefined, restingEnergyCals: undefined, soundLevel: undefined, userId: NaN, + user: { + create: UserCreateWithoutDailyWatchDataInput | UserUncheckedCreateWithoutDailyWatchDataInput, + connectOrCreate: UserCreateOrConnectWithoutDailyWatchDataInput, + connect: UserWhereUniqueInput + } } })
Argument user
is missing.
all the fields were undefined and user had no number (NaN ). We fixed this by adding convert.js below, here is part of it:
const main = async() => {
for (let i in lines.length){
console.log(`Data ${ i } of ${ watchData.length }`)
const dataPoint = lines[i].split(',');
const jsonBody = {};
jsonBody["steps"] = dataPoint["dataPoint"];
jsonBody["distance"] = dataPoint["dataPoint"];
jsonBody["flights"] = dataPoint["dataPoint"];
jsonBody["activeEnergyCals"] = dataPoint["dataPoint"];
jsonBody["handwashingSeconds"] = dataPoint["dataPoint"];
jsonBody["restingEnergyCals"] = dataPoint["dataPoint"];
jsonBody["soundLevel"] = dataPoint["dataPoint"];
jsonBody["userId"] = id;
another error message I got was this:
Server listening on port 5000
POST /signin 401 465.060 ms - 28
Error: data and hash arguments required
at Object.compare (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/bcrypt/bcrypt.js:208:17)
at /Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/bcrypt/promises.js:29:12
at new Promise (<anonymous>)
at module.exports.promise (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/bcrypt/promises.js:20:12)
at Object.compare (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/bcrypt/bcrypt.js:204:25)
at exports.comparePasswords (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/modules/auth.js:6:19)
at exports.signin (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/user.js:43:31)
POST /signin 500 129.231 ms - 35
This error was because passwordHash was defined in my schema, but I was using password in my code.
const isValid = await comparePasswords(req.body.password, user.passwordHash);
if (!isValid) {
res.status(401).json({ message: 'Invalid credentials' });
return;
}
These are some lines from my user.js, before the fix, user.passwordHash
was user.password
.
After many hours of work with help from Paul, we made good progress but got an error. I will note it here for the next time I return.
TypeError: Cannot read properties of undefined (reading 'match')
at parseNumericPattern (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/date-fns/parse/_lib/utils.js:27:34)
at parseNDigits (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/date-fns/parse/_lib/utils.js:84:14)
at MonthParser.parse (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/date-fns/parse/_lib/parsers/MonthParser.js:43:35)
at MonthParser.run (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/date-fns/parse/_lib/Parser.js:7:25)
at parse (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/date-fns/parse.js:442:34)
at exports.createDailyWatchData (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/handlers/dailyWatchData.js:7:24)
at Layer.handle [as handle_request] (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/express/lib/router/route.js:149:13)
at Route.dispatch (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/express/lib/router/route.js:119:3)
at Layer.handle [as handle_request] (/Users/torstenspieler/workspace/upper-division-cs/web-24wi/projects/AppleWatchHeathData/api-server/node_modules/express/lib/router/layer.js:95:5)
POST /daily-Watch-Data 500 8.694 ms - 35