fac24 / Week-4-Sonia-Peter-George-Petra

COCORICO 🧀
http://cocorico.herokuapp.com
2 stars 1 forks source link

possible reasons why app is crashing when i try to add a post #31

Open HusJAW opened 2 years ago

HusJAW commented 2 years ago

it is allowed to have multiple inputs with the same name , only id 'smust be unique. I wouldn't recommend it however, why even put yourself in a position to be confused down the road. The name attribute only defines what each form field element will be represented as when sent to the server

<input type="radio" id="recipeUrl" **name="recipe"** value="recipeURL"/>
  <label for="recipeUrl">URL: <input type="text" name="recipeURL"/></label>

  <input type="radio" id="recipeSteps" **name="recipe"** value="recipeSteps"/>
  <label for="recipeSteps"> Recipe Steps <textarea name="recipeSteps"></textarea></label>

const form = request.body; const recipeType = form.recipe; // 1 Get user id console.log(form[recipeType]); const sid = request.signedCookies.sid; const user = await model.getSession(sid); // 2 Add post await model.addPost(user.id, form.dish, form[recipeType], form.joke);

not sure if form[recipeType] is why your app is crashing

maybe add an if check

if(recipeType === recipeURL) { await model.addPost(user.id, form.dish, form.recipeURL , form.joke); }else { await model.addPost(user.id, form.dish, form.recipeSteps , form.joke); }

sonianb commented 2 years ago

We're using the same name because these are radio buttons, so the idea is that users will be able to select only one of the two buttons and the selected button will give us the value (the button that has not been selected will be ignored by default).