Reputation: 3444
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\\\"}]\""
Upvotes: 0
Views: 1958
Reputation: 11330
The error you are getting is due to myOwnObject
being undefined. You need to initialise object before you can start setting their properties. The error you are getting mentions that
Cannot set property 'name' of undefined
It means myOwnObject
is not defined but you're trying to set myOwnObject.name
.
Initialise your object like this -
var myOwnObject = {};
instead of
var myOwnObject;
Error should be gone now.
Update - For the second error -
The same thing as above is also true for arrays. Your dataArray
is not initialised. You need to initialise it like this -
var dataArray = [];
Update 2 -
You are getting the \
in your output because you are converting your JSON output to a single string. To send the JSON array directly, remove the JSON.stringify()
method. Send the response like this -
res.json(dataArray);
Upvotes: 1