I am implementing server related API using NodeJs and MongoDB with mongoose Schema based model. So My previous question was here
'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);
}
// console.log('Web Server started, waiting for connections...'+task);
res.json(task);
});
};
When i run the server it returns the jsonArray like this,
[
{
"_id": "58ba4c9c03e10b16d140775f",
"name": "Noorul ahamed",
"__v": 0,
"status": [
"pending"
],
"Created_date": "2017-03-04T05:11:56.590Z"
},
....
...
...
]
here my doubt is,
in that above code, task gives this kind of response. how do i get the every object in that task array? do i need to use for loop to get all object from that array? I am new to NodeJs and MongoDB. I structured well server.Now I am stuck with this.? Help will be appreciated.
UPDATE
// var myOwnObject = new mongoose.Schema({ name: 'string', id: 'string',status:'string',createdDate:'String' });
// var myOwnObject = {};
var dataArray;
for (var i = 0; i < task.length; i++) {
var userObject = task[i];
myOwnObject.name=userObject.name;
myOwnObject.id=userObject._id;
myOwnObject.status=userObject.status[0];
myOwnObject.createdDate=userObject.Created_date;
dataArray[i]=myOwnObject;
}
res.json(JSON.Stringify(dataArray));
Error
events.js:161 throw er; // Unhandled 'error' event ^
TypeError: Cannot set property 'name' of undefined at /home/noorulhasan/todoListApi/api/controllers/todoListController.js:17:29 at model.Query. (/home/noorulhasan/node_modules/mongoose/lib/model.js:3433:16) at /home/noorulhasan/node_modules/kareem/index.js:273:21 at /home/noorulhasan/node_modules/kareem/index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9) [nodemon] app crashed - waiting for file changes before starting...
UPDATE ERROR When i tried with myObject={}
events.js:161 throw er; // Unhandled 'error' event ^
TypeError: Cannot set property '0' of undefined at /home/noorulhasan/todoListApi/api/controllers/todoListController.js:21:25 at model.Query. (/home/noorulhasan/node_modules/mongoose/lib/model.js:3433:16) at /home/noorulhasan/node_modules/kareem/index.js:273:21 at /home/noorulhasan/node_modules/kareem/index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)
UPDATE
I am getting this kind of output. how to get rid of '\' symbol.
"[{\"name\":\"Ahamed\",\"id\":\"58bac0fc2cf0894f071e41c0\",\"createdDate\":\"2017-03-04T13:28:28.759Z\"}]
I tried following way.
var jsonData=JSON.stringify(dataArray);
var updateJson = jsonData.replace('"\"', "");
res.json(JSON.stringify(updateJson));
but it adds extra symbols like below,
"\"[{\\\"name\\\":\\\"Ahamed\\\",\\\"id\\\":\\\"58bac0fc2cf0894f071e41c0\\\",\\\"status\\\":\\\"pending\\\",\\\"createdDate\\\":\\\"2017-03-04T13:28:28.759Z\\\"}]\""
task
is just a normal array. You can access elements inside it like you would with any normal array. You can select a particular element withtask[n]
or use any kind of loop or iterator functions with it. - Jyotman Singh