I want to do a basic editing of data stored in the database (mongoDB). Here is what I've done:
angular service
createReview(review) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers })
.subscribe(res => {
console.log(res.json());
});
}
editReview(review) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers })
.subscribe(res => {
console.log(res.json());
});
}
deleteReview(id) {
this.http.delete('http://localhost:3000/api/reviews/' + id).subscribe((res) => {
console.log(res.json());
});
}
in server.js
// Set up
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var ObjectId = require('mongodb').ObjectID; // objectID
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
var cors = require('cors');
// Configuration
mongoose.connect('mongodb://localhost/data');
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use(cors());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Models
var Review = mongoose.model('Review', {
title: String,
description: String,
rating: Number
});
// Routes
// Get reviews
app.get('/api/reviews', function(req, res) {
console.log("fetching reviews");
// use mongoose to get all reviews in the database
Review.find(function(err, reviews) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(reviews); // return all reviews in JSON format
});
});
// create review and send back all reviews after creation
app.post('/api/reviews', function(req, res) {
if (req.params.review_id == undefined) {
console.log("creating review");
// create a review, information comes from request from Ionic
Review.create({
title: req.body.title,
description: req.body.description,
rating: req.body.rating,
done: false
}, function(err, review) {
if (err)
res.send(err);
// get and return all the reviews after you create another
Review.find(function(err, reviews) {
if (err)
res.send(err)
res.json(reviews);
});
});
} else {
console.log("editing review")
var query = ObjectId(req.params.review_id);
// update a review, information comes from request from Ionic
Review.findOneAndUpdate(query, {
title: req.body.title,
description: req.body.description,
rating: req.body.rating,
done: false
}, function(err, review) {
if (err)
res.send(err);
// get and return all the reviews after you create another
Review.find(function(err, reviews) {
if (err)
res.send(err)
res.json(reviews);
});
});
}
});
// delete a review
app.delete('/api/reviews/:review_id', function(req, res) {
console.log("deleting review");
Review.remove({
_id: req.params.review_id
}, function(err, review) {
});
});
// listen (start app with node server.js) ======================================
app.listen(3000);
console.log("App listening on port 3000");
Creating a new review works just fine and also deleting a review. But when it comes to editing I get the undefined error from req.params.review_id in the app.post function in server.js. I'm sure I'm not doing this in a proper way. How to do an update of data using these libraries and frameworks?