Giter Site home page Giter Site logo

jaggerabney / nodejs-the-complete-guide-code Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 5.46 MB

A repository containing all of the code I wrote while going through the "NodeJS - The Complete Guide (MVC, REST APIs, GraphQL, Deno)" course on Udemy.

JavaScript 70.17% HTML 5.38% CSS 9.87% Pug 8.41% Handlebars 1.08% EJS 1.44% TypeScript 3.65%
express express-graphql express-rest-api express-validator nodejs

nodejs-the-complete-guide-code's Introduction

Node.js - The Complete Guide Code

This repository contains all of the code for Maximilian Schwarzmüller's Udemy Course, "Node.js - The Complete Guide (MVC, REST APIs, GraphQL, Deno)". Projects in this repo do not necessarily correlate to sections in the course.

nodejs-the-complete-guide-code's People

Contributors

jaggerabney avatar

Stargazers

 avatar  avatar

Watchers

 avatar

nodejs-the-complete-guide-code's Issues

Posts added via websocket connection do not have the creator's username

This is because the creator's information is only populated in the exports.getPosts method in the feed controller. To fix this, change exports.createPost from this:

io.get().emit("posts", { action: "create", post });

...to this:

io.get().emit("posts", {
    action: "create",
    post: {
        ...post._doc,
        creator: { _id: req.userId, name: creator.name },
    },
});

Adding a post causes an error

The error message is rendered as a modal on the frontend and reads as follows:

Cannot read properties of undefined (reading '_id')

I'm not sure why this happens.

Username is not shown in posts

The user that created a post does not show in both the feed and the single post view. This is because the "creator" field is not populated using the User model's populate method. Change these:

// --- exports.getPosts ---
const posts = await Post.find()
      // These functions are for pagination:
      // skip excludes documents and limit restricts how many are returned
      .skip((currentPage - 1) * paginationThreshold)
      .limit(paginationThreshold);
// --- exports.getPost ---
const post = await Post.findById(postId);

...to these:

// --- exports.getPosts ---
const totalItems = await Post.find().countDocuments();
    const posts = await Post.find()
      .populate("creator")
      // These functions are for pagination:
      // skip excludes documents and limit restricts how many are returned
      .skip((currentPage - 1) * paginationThreshold)
      .limit(paginationThreshold);
// --- exports.getPost ---
const post = await Post.findById(postId).populate("creator");

ReferenceError: loadedUser is not defined

This error occurs because you forgot to change the login function in the auth controller. Change this:

const token = jwt.sign(
      {
        email: loadedUser.email,
        userId: loadedUser._id.toString(),
      },
      process.env.JWT_KEY,
      { expiresIn: "1h" }
);

...to this:

const token = jwt.sign(
      {
        email: user.email,
        userId: user._id.toString(),
      },
      process.env.JWT_KEY,
      { expiresIn: "1h" }
);

Pagination initially doesn't work when pagination threshold is exceeded

This is because the total number of posts, which the pagination logic relies on, is not updated when the post is created. In finishEditHandler, update this:

this.setState((prevState) => {
  let updatedPosts = [...prevState.posts];
  
  if (prevState.editPost) {
    const postIndex = prevState.posts.findIndex(
      (p) => p._id === prevState.editPost._id
    );
  
    updatedPosts[postIndex] = post;
  } else {  
    if (prevState.posts.length >= 2) {
      updatedPosts.pop();
    }
  
    updatedPosts.unshift(post);
  }
  
  return {
    posts: updatedPosts,
    isEditing: false,
    editPost: null,
    editLoading: false,
  };
});

...to this:

this.setState((prevState) => {
  let updatedPosts = [...prevState.posts];
  let updatedTotal = prevState.totalPosts;
  
  if (prevState.editPost) {
    const postIndex = prevState.posts.findIndex(
      (p) => p._id === prevState.editPost._id
    );
  
    updatedPosts[postIndex] = post;
  } else {
    updatedTotal++;
  
    if (prevState.posts.length >= 2) {
      updatedPosts.pop();
    }
  
    updatedPosts.unshift(post);
  }
  
  return {
    posts: updatedPosts,
    isEditing: false,
    editPost: null,
    editLoading: false,
    totalPosts: updatedTotal,
  };
});

The main functionality of project-7 doesn't work

This is because there are no IDs specified for the two number inputs on the page. Change this:

<input type="number" />
<input type="number" />

...to this:

<input id="num1" type="number" />
<input id="num2" type="number" />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.