TheHonoredOne
TheHonoredOne
KPCKevin Powell - Community
Created by TheHonoredOne on 2/23/2024 in #front-end
Mantaining field alignment across rows
No description
9 replies
KPCKevin Powell - Community
Created by TheHonoredOne on 2/9/2024 in #back-end
Trying to update a single movie property with PATCH
Hello, I'm building a REST API and I'm implementing a patchOneMovieById method, that takes an id and a an object of properties to update, I've split my code in the following files: movie.router.ts, movie.controller.ts, movie.service.ts and movie.repository.ts Here's the code for the service and repository:
// MovieService Class
patchOneById(id: Number, updatedMovieProps: Partial<Movie>) {
const movie = this.getOneById(id);

if (!movie) {
return null;
}

const updatedMovie: Movie = { ...movie, ...updatedMovieProps };
const updatedMovieInstance = this.movieRepository.updateOneById(id, updatedMovie);

return updatedMovieInstance;
}
// MovieService Class
patchOneById(id: Number, updatedMovieProps: Partial<Movie>) {
const movie = this.getOneById(id);

if (!movie) {
return null;
}

const updatedMovie: Movie = { ...movie, ...updatedMovieProps };
const updatedMovieInstance = this.movieRepository.updateOneById(id, updatedMovie);

return updatedMovieInstance;
}
// MovieRepository Class
updateOneById(id: Number, updatedMovie: Movie) {
const movieIndex = moviesData.findIndex((movie) => movie.id === id);

if (movieIndex === -1) {
return undefined;
}

const updatedMovieInstance: Movie = {
...moviesData[movieIndex],
...updatedMovie,
};

moviesData[movieIndex] = updatedMovieInstance;

return updatedMovieInstance;
}
// MovieRepository Class
updateOneById(id: Number, updatedMovie: Movie) {
const movieIndex = moviesData.findIndex((movie) => movie.id === id);

if (movieIndex === -1) {
return undefined;
}

const updatedMovieInstance: Movie = {
...moviesData[movieIndex],
...updatedMovie,
};

moviesData[movieIndex] = updatedMovieInstance;

return updatedMovieInstance;
}
The issue is, when I update a single property on a movie, the other properties are deleted and I get an output such as:
{
"id": 1,
"summary": "I patched the summary only"
},
{
"id": 2,
"name": "movie2",
"summary": "summary 2",
"trailerURL": "https://www.youtube.com/"
},
{
"id": 1,
"summary": "I patched the summary only"
},
{
"id": 2,
"name": "movie2",
"summary": "summary 2",
"trailerURL": "https://www.youtube.com/"
},
1 replies
KPCKevin Powell - Community
Created by TheHonoredOne on 1/20/2023 in #front-end
Working with text width
7 replies
KPCKevin Powell - Community
Created by TheHonoredOne on 1/11/2023 in #front-end
Error concatenating strings in a for loop
Hello, I'm trying to generate a random string of length 12
let id = '';

for(let i = 0; i < 12; i++) {
let randNum = Math.floor(Math.random() * (128 - 21) + 21);
let char = String.fromCharCode(randNum);
id += char;
}
let id = '';

for(let i = 0; i < 12; i++) {
let randNum = Math.floor(Math.random() * (128 - 21) + 21);
let char = String.fromCharCode(randNum);
id += char;
}
What I'm trying to do is, first, generate a random character, using the ASCII table starting at 21 till 127. Then with the String.fromCharCode, I get the character equivalent of the number generated. The idea is that, I have an empty string and I use the += operator to concatenate whatever char I get from the loop, easy peasy lemon squeezy, nothing out of the ordinary, BUT, and this may be a conceptual mistake, when I run this code I get a reference error saying that id has already BEEN DECLARED but I don't see myself redeclaring it so I don't understand. Also, I've tried with the .concat method and it still gives the same result. Using const doesn't work either.
7 replies
KPCKevin Powell - Community
Created by TheHonoredOne on 12/17/2022 in #front-end
JS Ignoring my if condition
I'm trying to add the value of a selected radio button to a span after clicking a submit button. Here's what my HTML looks like
<div class="ratings">
<div>
<input type="radio" id="one" name="rating" value="1">
<label for="one" class="rating"><span class="rating__content">1</span></label>
</div>

<div>
<input type="radio" id="two" name="rating" value="2">
<label for="two" class="rating"><span class="rating__content">2</span></label>
</div>
</div>
<button class="button" id="submit-button">Submit</button>
</div>
<div class="ratings">
<div>
<input type="radio" id="one" name="rating" value="1">
<label for="one" class="rating"><span class="rating__content">1</span></label>
</div>

<div>
<input type="radio" id="two" name="rating" value="2">
<label for="two" class="rating"><span class="rating__content">2</span></label>
</div>
</div>
<button class="button" id="submit-button">Submit</button>
</div>
And here's the JS for this:
'use strict'

const card = document.querySelector(".rating-card");
const cardFront = document.querySelector(".rating-card__front");
const cardBack = document.querySelector(".rating-card__back");
const submitBtn = document.getElementById("submit-button");

submitBtn.addEventListener('click', function (){
const selectedRating = document.querySelector('input[type=radio]:checked').value;
if(selectedRating !== null) {
const selectionOutput = document.querySelector(".selected-value");
cardFront.classList.add('hidden');
cardBack.classList.remove('hidden');
selectionOutput.innerHTML = selectedRating;
}
});
'use strict'

const card = document.querySelector(".rating-card");
const cardFront = document.querySelector(".rating-card__front");
const cardBack = document.querySelector(".rating-card__back");
const submitBtn = document.getElementById("submit-button");

submitBtn.addEventListener('click', function (){
const selectedRating = document.querySelector('input[type=radio]:checked').value;
if(selectedRating !== null) {
const selectionOutput = document.querySelector(".selected-value");
cardFront.classList.add('hidden');
cardBack.classList.remove('hidden');
selectionOutput.innerHTML = selectedRating;
}
});
I'm getting the following error: Cannot set properties of null (setting 'innerHTML') So I don't understand why I'm getting that error if the .innerHTML part should only be applied if the selectedRating is not null
27 replies