Giter Site home page Giter Site logo

rest-api-playlist's Introduction

rest-api-playlist

Course files for the REST API tutorial series on The Net Ninja Youtube channel

How to use

Each branch corresponds to a specific lesson in the playlist. For example the lesson-10 branch will contain the code at the END of lesson 10.

Not every lesson has a corresponding branch (e.g. where the lesson added nothing else to the code).

rest-api-playlist's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest-api-playlist's Issues

For updating

Book.findByIdAndUpdate(req.params.id, req.body, { new: true }, function (err, book) {
    if (err) {
      next(err)
    } else {
      res.json(book)
    }
  })

GeoJSON

You didn't include the GeoJSON object file

React with ES6

Hi, I wanted to write the code in ES6, however nothing is being returned. Can someone check my code please?

  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>


<script type="text/babel">
class Ninjas extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      ninjas: []
    };
  }

  handleSubmit(e) {
    e.preventDefault();
    let lng = this.refs.lng.value;
    let lat = this.refs.lat.value;

    fetch(`/api/ninjas?lng=${lng}&lat=${lat}`)
      .then(data => {
        return data.json();
      })
      .then(json => {
        this.setState({
          ninjas: json
        });
      });
  }

  render() {
    let ninjas = this.state.ninjas;

    ninjas = ninjas.map((ninja, index) => {
      return (
        <li key={index}>
          <span className="name">{ninja.obj.name}></span>
          <span className="rank">{ninja.obj.rank}</span>
          <span className="dist">{Math.floor(ninja.dis / 1000)} km</span>
        </li>
      );
    });

    return (
      <div id="ninja-container">
        <form id="search" onSubmit={this.handleSubmit.bind(this)}>
          <label>Enter your latitude</label>
          <input type="text" ref="lat" placeholder="latitude" required />
          <label>Enter your longitude</label>
          <input type="text" ref="lng" placeholder="longitude" required />
          <input type="submit" value="Find Ninjas" />
        </form>
        <ul>{ninjas}</ul>
      </div>
    );
  }
}

ReactDOM.render(<Ninjas />, document.getElementById("ninjas"));
</script>

"geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 100000.0"

"geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 100000.0"
that is my error and i can't handle , my code is =>

router.get('/ninjas', (req, res, next) => {
Ninja.aggregate().near({
near: { type: "Point", coordinates: [parseFloat(req.query.lng), parseFloat(req.query.lat)] },
distanceField: "dist.calculated",
maxDistance: 100000,
spherical: true
}).then(function(ninjas) {
res.send(ninjas);
}).catch(next);

})

const GeoSchema = new Schema({
type: {
type: String,
default: 'Point'
},
coordinates: {
type: [Number],
index: '2dsphere'
}
});

const NinjaSchema = new Schema({
name: {
type: String,
required: [true, 'Name field is required']
},
rank: {
type: String
},
available: {
type: Boolean,
default: false
},
geometry: GeoSchema

});

.map no longer working

Hi Shaun,

I am really enjoying the rest api playlist, thank you very much. Very glad to see you are now on Udemy!

I am just at the last part now and I am getting errors around the .map section of the public index.html file:
render: function(){
var ninjas = this.state.ninjas;
ninjas = ninjas.map(function(ninja, index){
return(



  • {ninja.obj.name}
    {ninja.obj.rank}
    {Math.floor(ninja.dis / 1000)} km

  • );
    });

    My browser is saying .map is not a supported function.

    Do you have any feedback?

    Many thanks,
    Charlie

    Type error

    Ninja.create is not working showing an error that ninja.create is not a function

    console.log() not working

    The command console.log() is not working.
    No outputs are seen when the index.js file is run on the terminal.

    index.js file:
    screen shot 2018-04-01 at 11 58 46 pm

    Terminal:
    screen shot 2018-04-01 at 11 59 09 pm

    Please provide a solution to this.

    Frontend Authentication

    Hi, Shaun,
    Your RESTful API tutorial is great. But I have a question about the authentication.

    It seems your tutorial does not verify the frontend. I mean, it seems anyone could make a request and retrieve data from the api.

    I understand your tutorial is just a demo. So, do I need add some verification middleware and do some verification before the api sends data back to the frontend?

    Thanks.

    Cannot read property 'available' of undefined

    Uncaught (in promise) TypeError: Cannot read property 'available' of undefined
    at :20:68
    at Array.map ()
    at Object.render (:15:25)
    at p._renderValidatedComponentWithoutOwnerOrContext (react-dom.min.js:11)
    at p._renderValidatedComponent (react-dom.min.js:11)
    at p._updateRenderedComponent (react-dom.min.js:11)
    at p._performComponentUpdate (react-dom.min.js:11)
    at p.updateComponent (react-dom.min.js:11)
    at performUpdateIfNecessary (react-dom.min.js:11)
    at Object.performUpdateIfNecessary (react-dom.min.js:12)

    ValidationError: name: Name field is required

    Great stuff but get stuck on chapter 9.

    Trying to POST in postman to http://localhost:4000/api/ninjas with body:

    {
    "name": "Ninja"
    }

    but it seems that the POST does not send the body since my consoles logs:
    (node:14148) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): ValidationError: name: Name field is required

    any clue why validation fails spite having a name in the body=

    Lesson 14, geoNear depreciated, .aggregate is used.

    // get a list of ninjas from the db
    router.get('/ninjas', function(req, res, next){

    Ninja.aggregate(
        [
    {$geoNear:{
        near:{
            type: 'Point', 
            coordinates: [parseFloat(req.query.lng), parseFloat(req.query.lat)]
        },
        maxDistance: 100000, 
        spherical: true,
        distanceField: 'String'
    }
    }]).then(function(ninjas){
        res.send(ninjas);
    }).catch(next);
    

    });

    https://stackoverflow.com/questions/66611269/mongodb-geonear-not-working-for-some-coordinates-but-working-for-others

    I followed along with rewriting the code from a stackoverflow post, along with prompts from Postman and the Code Editor. I see that there are also other answers to the geoNear not working, this is the solution that worked for me.

    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.