I was following a tutorial(https://medium.com/weekly-webtips/building-restful-apis-with-node-js-and-express-a9f648219f5b) to build an API but it’s from 2 years ago and I can’t quite seem to change the code to accommodate more recent updates in the library. I’m able to get the get request to work but not the post. When I do
.post(addNewCoordinates);
I get “Error: Route.post() requires a callback function but got a [object Undefined]”
Which the internet said to add async await which I did. Am I missing something else??
The setup I have with the mongodb is I created a database in the compass GUI and that’s literally it.(i technically have a collection in the database)
Here is my index.js
import express from 'express';
import routes from './src/routes/coordinatesRoutes';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
const app = express();
routes(app)
const PORT = 1234;
// Will print at localhost:{Port}
app.get('/', (req, res) =>
res.send(`Node and express server running on port ${PORT}`)
)
// Will print in terminal
app.listen(PORT, () =>
console.log(`Your server is running on port ${PORT}`))
// Database Setup
// mongoose connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/coordinates', {
useNewUrlParser: true,
useUnifiedTopology: true
})
//bodyparser setup
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
coordinatesController.js:
import mongoose from 'mongoose';
import { InputDataSchema } from '../models/coordinatesModel'
const InputData = mongoose.model('InputData', InputDataSchema);
const addNewCoordinates = async (req,res) => {
let newCoordinates = new InputData(req.body);
await newCoordinates.save((err, coordinates) => {
if (err) {
res.send(err)
}
res.json(coordinates)
})
}
export default addNewCoordinates;
coordinatesModel.js:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
export const InputDataSchema = new Schema({
latitude: {
type: Number,
required: "Enter latitude"
},
longitude: {
type: Number,
required: "Enter longitude"
},
altitude: {
type: Number,
required: "Enter altitude"
},
ecef: {
type: Array
},
created_date: {
type: Date,
default: Date.now
}
})
coordinatesRoute.js
import { addNewCoordinates } from '../controllers/coordinatesController'
const routes = (app) => {
//create route for donations
app.route('/lla')
//create get request
.get((req, res) =>
res.send('GET request successful!'))
//create post request
.post((req, res) =>
res.set('POST request successful!'));
// This crashes and gives me a Route.post() requires a callback function but got a [object Undefined] so I'm just trying to focus on getting a post without really any data.
//.post(addNewCoordinates);
// create a new route so you can get these donation entries by their ID's
app.route('/lla/:timestamp')
//create put request
.put((req, res) =>
res.send('PUT request successful!'))
//create delete request
.delete((req, res) =>
res.send('DELETE request successful'))
}
export default routes;
Thanks for any help!!