I created a REST Api where I can perform GET/POST/PUT/DELETE requests through postman. He is the json I am getting.
[
{
"_id": "593978f7995e071df49b08d8",
"updatedAt": "2017-06-08T16:19:03.551Z",
"createdAt": "2017-06-08T16:19:03.551Z",
"title": "Camping in the City and this year.",
"image": "img/news1.jpg",
"description": "From the Municipality of Larissa, and in particular the Social Policy Advocacy and the Department of Sports and Culture and Social Policy, it is announced that following the absolutely successful 'Camping in the City' program, organized last summer, the Municipality of Larissa in collaboration with the citys stakeholders, the program Will be implemented this year as well.",
"__v": 0,
"comments": [
{
"updatedAt": "2017-06-08T16:19:03.547Z",
"createdAt": "2017-06-08T16:19:03.547Z",
"rating": 5,
"comment": "Λατρεύω το camping!!!",
"author": "Theo",
"_id": "593978f7995e071df49b08db"
},
{
"updatedAt": "2017-06-08T16:19:03.550Z",
"createdAt": "2017-06-08T16:19:03.550Z",
"rating": 5,
"comment": "Καλά μιλάμε τέλειο!!!",
"author": "Chris",
"_id": "593978f7995e071df49b08da"
},
{
"updatedAt": "2017-06-08T16:19:03.550Z",
"createdAt": "2017-06-08T16:19:03.550Z",
"rating": 2,
"comment": "Βαρετό.",
"author": "Σοφία.",
"_id": "593978f7995e071df49b08d9"
}
]
}
]
I can also confirm that the above array object in stored in mongodb by typing in Node.js terminal the command db.news.find().pretty(). And here is my code for achieving this.
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
var News = require('../models/news')
var newsRouter = express.Router();
newsRouter.use(bodyParser.json());
newsRouter.use(cors);
newsRouter.route('/')
.get(function(req,res,next){
News.find({})
.exec(function (err, news) {
if (err) throw err;
res.json(news);
});
})
.post(function(req,res,next){
News.create(req.body,function(err,news){
if(err) throw err;
console.log('News created')
var id = news._id;
res.writeHead(200,{
'Content-Type':'text/plain'
});
res.end('Added the article with id: ' + id);
})
})
.delete(function (req, res, next) {
News.remove({}, function (err, resp) {
if (err) throw err;
res.json(resp);
});
});
newsRouter.route('/:newsId')
.get(function (req, res, next) {
req.params.newsId = mongoose.Types.ObjectId(req.params.newsId);
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
console.log(req.params.newsId);
res.json(news);
});
})
.put(function (req, res, next) {
News.findByIdAndUpdate(req.params.newsId, {
$set: req.body
}, {
new: true
}, function (err, news) {
if (err) throw err;
res.json(news);
});
})
.delete(function (req, res, next) {
News.findByIdAndRemove(req.params.newsId, function (err, resp) {
if (err) throw err;
res.json(resp);
});
});
newsRouter.route('/:newsId/comments')
.get(function (req, res, next) {
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
res.json(news.comments);
});
})
.post(function (req, res, next) {
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
news.comments.push(req.body);
news.save(function (err, news) {
if (err) throw err;
console.log('Updated Comments!');
res.json(news);
});
});
})
.delete(function (req, res, next) {
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
for (var i = (news.comments.length - 1); i >= 0; i--) {
news.comments.id(news.comments[i]._id).remove();
}
news.save(function (err, result) {
if (err) throw err;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Deleted all comments!');
});
});
});
newsRouter.route('/:newsId/comments/:commentId')
.get(function (req, res, next) {
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
res.json(news.comments.id(req.params.commentId));
});
})
.put(function (req, res, next) {
// We delete the existing commment and insert the updated
// comment as a new comment
News.findById(req.params.newsId, function (err, news) {
if (err) throw err;
news.comments.id(req.params.commentId).remove();
news.comments.push(req.body);
news.save(function (err, news) {
if (err) throw err;
console.log('Updated Comments!');
res.json(news);
});
});
})
.delete(function (req, res, next) {
News.findById(req.params.newsId, function (err, news) {
news.comments.id(req.params.commentId).remove();
news.save(function (err, resp) {
if (err) throw err;
res.json(resp);
});
});
});
module.exports = newsRouter;
No I want the client to read the news with Angular.Js. For that I created a factory
angular.module('larissaApp')
.constant('baseURL', 'http://localhost:3000/')
.factory('newsFactory',['$resource', 'baseURL', function($resource,baseURL) {
return $resource(baseURL + "news/:id", null, {
'update': {
method: 'PUT'
}
});
}]);
and a controller.
angular.module('larissaApp')
.controller('IndexController',['$scope','newsFactory','AnnouncementsFactory','EventsFactory',function($scope,newsFactory,AnnouncementsFactory,EventsFactory){
$scope.message="Loading ...";
var news_one = newsFactory.query({})
.$promise.then(
function(response){
var news_one = response;
$scope.newsOne = news_one[0];
},
function(response) {
$scope.message = "Error: "+response.status + " " + response.statusText;
}
);
...
}]);
However nothing is shown in my browser. I suspect that I am doing something wrong in my controller. Any ideas?
Thanks,
Theo.
via Theo
No comments:
Post a Comment