iamshaunjp / rest-api-playlist Goto Github PK
View Code? Open in Web Editor NEWCourse files for the REST API tutorial series on The Net Ninja Youtube channel
Course files for the REST API tutorial series on The Net Ninja Youtube channel
// 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);
});
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.
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(
My browser is saying .map is not a supported function.
Do you have any feedback?
Many thanks,
Charlie
rest-api-playlist/routes/api.js
Line 10 in 8d3fb58
react.js:2231 Warning: Accessing createClass via the main React package is deprecated, and will be removed in React v16.0.
https://reactjs.org/blog/2017/04/07/react-v15.5.0.html#migrating-from-react.createclass
lesson 1 missing from the course
Ninja.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ parseFloat(req.query.lng) , parseFloat(req.query.lat) ] },
maxDistance: 100000,
distanceField: "distance",
spherical: true
}
}
Hi how can i find your used Css file? Because there is none!
"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
});
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=
Ninja.create is not working showing an error that ninja.create is not a function
(node:10932) DeprecationWarning: open()
is deprecated in mongoose >= 4.11.0, use openUri()
instead, or set the useMongoClient
option if using connect()
or createConnection()
. See http://mongoosejs.com/docs/connections.html#use-mongo-client
This is resolved by:
mongoose.connect('mongodb://localhost/ninjago', {useMongoClient: true});
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.
Hey Ninja, you are the best ๐ฏ ๐ Can you please check this issue of body-parser
expressjs/body-parser#328
You didn't include the GeoJSON object file
Looks like it's empty
Hey Ninja, can you please make a video explaining anything starts with pre
like: preload
, prefetch
, preconnect
, precompile
and also the lazyLoad concept?
Book.findByIdAndUpdate(req.params.id, req.body, { new: true }, function (err, book) {
if (err) {
next(err)
} else {
res.json(book)
}
})
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)
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>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.