I am new to NodeJS and MongoDb. I am facing an issue on jsonObject.
Step 1: I have installed NodeJS and MongoDB sucessfully in my ubuntu 16.04 system.
Step 2: I created the all server setup with package.json file in my project folder.
Step 3: I created the server.js file Like below.
server.js File
express = require('express'),
routes = require('./api/routes/todoListRoutes');
mongoose = require('mongoose'),
Task = require('./api/models/todoListModels'),
bodyParser = require('body-parser');
app = express(),
port = process.env.PORT || 3000,
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
routes(app);
app.listen(port);
console.log('todo list RESTful API server started on: ' + port);
then I created Model file for storing the records.
todoListModels.js
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TaskSchema = new Schema({
name: {
type: String,
Required: 'Kindly enter the name of the task'
},
Created_date: {
type: Date,
default: Date.now
},
status: {
type: [{
type: String,
enum: ['pending', 'ongoing', 'completed']
}],
default: ['pending']
}
},{ versionKey: false }//Updated);
module.exports = mongoose.model('Tasks', TaskSchema);
todoListRoutes.js
'use strict';
module.exports = function(app) {
var todoList = require('../controllers/todoListController');
app.route('/tasks').get(todoList.list_all_tasks).post(todoList.create_a_task);
app.route('/tasks/:taskId').get(todoList.read_a_task).put(todoList.update_a_task).delete(todoList.delete_a_task);
};
todoListController.js
'use strict';
mongoose = require('mongoose'),
Task = mongoose.model('Tasks');
exports.list_all_tasks = function(req, res) {
Task.find({}, function(err, task) {
if (err)
res.send(err);
res.json(task);
});
};
exports.create_a_task = function(req, res) {
var new_task = new Task(req.body);
new_task.save(function(err, task) {
if (err)
res.send(err);
res.json(task);
});
};
exports.read_a_task = function(req, res) {
Task.findById(req.params.taskId, function(err, task) {
if (err)
res.send(err);
res.json(task);
});
};
exports.update_a_task = function(req, res) {
Task.findOneAndUpdate(req.params.taskId, req.body, {new: true}, function(err, task) {
if (err)
res.send(err);
res.json(task);
});
};
exports.delete_a_task = function(req, res) {
Task.remove({
_id: req.params.taskId
}, function(err, task) {
if (err)
res.send(err);
res.json({ message: 'Task successfully deleted' });
});
};
Then i ran the node server by nodemon server.js Server successfully ran. then, I try to get the data from the database using POST MAN Application.
So, I did like below,
GET method: localhost:3000/tasks
it successfully ran and it produced result.
like below,
[
{
"_id": "58ba4c9c03e10b16d140775f",
"name": "Karthik",
"__v": 0,
"status": [
"pending"
],
"Created_date": "2017-03-04T05:11:56.590Z"
}]
My problem is here,
1)I didn't created the __v and id fields here. Then why it is coming? 2)Then i need proper date format in Created_date field. like "dd-MM-yyyy hh:mm". How to do it?
Help will be appreciated.Thank you.
UPDATE
When i try to install moment, following error occurs
notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})