how to convert array api response to json in JavaScript - javascript

I have an api which return data structured like this :
//code
return response.status(200).json(startdate)
results data:
[
"2020-01-16",
"2020-01-18",
]
i want that this api to return result like this:
Code: 200
Content:
{
"availableDates": [
"2017-11-24",
"2017-11-27"
],
"status": "SUCCESS",
"message": ""
}
this is my full code where i get the data as array
app.get('/api/getBusyDays',(request, response) =>{
odoo.connect(function (err) {
console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
var params1 = [];
params1.push(inParams1);
console.log(' search params '+ JSON.stringify(params1));
odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
if (err) { return console.log(err) }
if(value) {
if (value.length > 0) {
value.forEach(function(a) {
a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
});
const startdate = [...new Set(value.map(val => val.start_datetime))];
startdate.sort();
// return response.status(200).json( value)
return response.status(200).json(startdate)
}
}

you can just create an object like this:
let arrVal = [
"2017-11-24",
"2017-11-27"
];
// return Object or your framework (Express or KOA or) response Object
console.log({
"Code": 200,
"Content": {
"availableDates": arrVal,
"status": "SUCCESS",
"message": ""
}
})
Update:
Based on the comments on this answer, modify your response so it looks like this:
app.get('/api/getBusyDays',(request, response) =>{
odoo.connect(function (err) {
console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
var params1 = [];
params1.push(inParams1);
console.log(' search params '+ JSON.stringify(params1));
odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
if (err) { return console.log(err) }
if(value) {
if (value.length > 0) {
value.forEach(function(a) {
a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
});
const startdate = [...new Set(value.map(val => val.start_datetime))];
startdate.sort();
// return response.status(200).json( value)
return response.status(200).json({"code": 200, "content": {"availableDates": startdate, "status": "SUCCESS", "message": ""}})
}
}

You can create an empty object and set properties to it and send it as response from ur node api service.
let data = [
"2017-11-24",
"2017-11-27"
];
let response = [];
response.data = data;
response.code = 200;
response.status = 'success';
response.message = 'Mission Successful';
return response;

Related

Async/Await : called function is not awiting and returning value

Hello I am creating 1 function with dynamic arguments where as I am calling api and on defined route I am calling express middleware function and from there I am calling another dynamic function which will help me to insert data into the database.
I am using Sequalize ORM
Here is code:
var async = require('async');
// Models
var LogSchema = require('../models/Logs')
module.exports = {
insertLog: async (req, res) => {
let result = await insertLogFn('1', 'method_name()', 'module_name_here', 'req.body', '{ api response }', 'action', '24')
console.log("result", result)
res.status(200).json(result)
}
};
function insertLogFn(status, invokedMethodName, moduleName, bodyRequest, apiResponse = null, actionName = null, userId) {
async.waterfall([
(nextCall) => {
let dataToBeInserted = {}
dataToBeInserted.status = status,
dataToBeInserted.invoked_method_name = invokedMethodName,
dataToBeInserted.module_name = moduleName,
dataToBeInserted.body_request = bodyRequest,
dataToBeInserted.api_response = apiResponse
dataToBeInserted.action_name = actionName,
dataToBeInserted.user_id = userId
LogSchema.create(dataToBeInserted).then(res => {
const dataObj = res.get({plain:true})
nextCall(null, {
status: 200,
message: "Log inserted successfully",
data: dataObj
})
}).catch(err => {
})
}
], (err, response) => {
if(err) {
}
return response
})
}
In module.export I have added insertLog function which is getting called in api and from there I am calling insertLogFn() which is declared outside of the module.export.
I am able to get inserted result in function insertLogFn() but the things is await is not working and not waiting for the result.
What I want to do is to wait till insertLogFn gets executed and the returned response has to be stored in the variable and return it as an api response.
You cannot. As per my understanding, IMO, Thumb rule is "Async/Await operation should return a promise"
function insertLogFn(status, invokedMethodName, moduleName, bodyRequest, apiResponse = null, actionName = null, userId) {
async.waterfall([
(nextCall) => {
let dataToBeInserted = {}
dataToBeInserted.status = status,
dataToBeInserted.invoked_method_name = invokedMethodName,
dataToBeInserted.module_name = moduleName,
dataToBeInserted.body_request = bodyRequest,
dataToBeInserted.api_response = apiResponse
dataToBeInserted.action_name = actionName,
dataToBeInserted.user_id = userId
LogSchema.create(dataToBeInserted).then(res => {
const dataObj = res.get({plain:true})
nextCall(null, {
status: 200,
message: "Log inserted successfully",
data: dataObj
})
return ;
console.log("you should return something here<-------");
}).catch(err => {
})
}
], (err, response) => {
if(err) {
}
return response
})
}
Now the answer will be clear if you read this one from Bergi: https://stackoverflow.com/a/40499150/9122159

returning null when using value out of block

I am trying to query one collection, get IDs from that and with that I am querying another collection and send the response. No issue in that. My issue is I am using foreach, so I am trying to get the value out of block and send the response. But it's throwing null. I consoled the value inside the block, its giving the response but I couldn't access the value out of the block.
Code
getDetails = async (req, res) => {
let data: any[] = [];
await employee.find({
_id: new mongoose.Types.ObjectId(req.body.id)
}, (err, obj) => {
if (err) {
console.log("No Such Employee")
} else {
obj.forEach(element => {
employer.find({
user: new mongoose.Types.ObjectId(element.user)
}, (err, details) => {
if (err) {
console.log("No such employer")
} else {
for (var i = 0; i < detail.length; i++) {
let p_info = {
"id": detail._id,
"fname": details[i].fname "lname": details[i].lname "phone": details[i].phone,
"email": details[i].email,
}
data.push(p_info); // I can get value here
}
}
})
});
res.send(data); // Cannot get value here
}
});
}
return response only when all your asynchronous db calls are done. see below:
getDetails = async(req, res) => {
let data = [];
let employees;
try {
employees = await employee.find({
_id: new mongoose.Types.ObjectId(req.body.id)
});
} catch (err) {
console.log("No Such Employee");
}
let emp_len = employees.length;
employees.forEach((emp_obj, idx) => {
let details = await employer.find({
user: new mongoose.Types.ObjectId(emp_obj.user)
});
for (var i = 0; i < patient.length; i++) {
let p_info = {
"id": patient._id,
"fname": details[i].fname,
"lname": details[i].lname,
"phone": details[i].phone,
"email": details[i].email,
}
data.push(p_info); // I can get value here
}
if (emp_len == idx + 1) {
res.send(data); // Cannot get value here
}
});
}

how can i print result array in node js outside mongoOp find query?

I am new in Node.js and I want to pass data array to controller. But I am unable to insert for loop data in array and I also want to get result data out side function.
router.get("/list-group", function(req, res) {
sess = req.session;
var response = {};
if (sess.loginData) {
var TableData = [];
var i = {};
var result = [];
mongoOp.users_group.find({
group_id: req.query.group_id
}, function(e, d) {
var len = d[0].assign_user_id.length;
var assignuserid = d[0].assign_user_id;
for (var i = 0; i < assignuserid.length; i++) {
var assignid = assignuserid[i];
mongoOp.users.find({
_id: assignid
}, function(err, data) {
// This is result array
result[i] = data;
})
}
// And I want to print result array here
console.log(result);
});
} else {
response = {
"error": true,
"message": "Invalid Login"
};
res.json(response);
}
})
I would make use of async and await
router.get('route', (req, res) => {
// ...
users.find(query, (err, d) => {
try {
// ...
var results = []
for (var data of array) {
const result = await fetchUser(data)
results.push(result)
}
console.log(results)
} catch (err) {
console.log('some error occured', err)
}
})
})
async function fetchUser(id) {
return new Promise((resolve, reject) => {
users.find({ _id: id }, (err, data) => {
return err ? reject(err) : resolve(data)
})
})
}
If you're not that familiar with async and await I would recommend this video
u need read about async and callbacks in javascript. An alternative is read about async and await.

how to handle expressJs callback and how to update object's property inside a function?

I have two js files. i am able to get data from mongodb by calliing bookDao.getActiveBookByCategoryId().
My Problem
In categoryDao.js file i am trying to update resultJson.book_countinside BookDao.getActiveBookByCategoryId() method. but it is not updating. So may i know how to fix this.
here book_count property in resultJson is still 0.
categoryDao.js
module.exports.getAllActiveCategory = (callback) => {
Category.find({
is_delete : false
}, (error, result) => {
if(error) {
console.log(error);
callback(commonUtil.ERROR);
}
if(result) {
var categoryArray = [];
for(var i=0; i<result.length; i++) {
var categorySingle = result[i];
var resultJson = {
_id : categorySingle._id,
category_name : categorySingle.category_name,
created_on : categorySingle.created_on,
book_count : 0
}
BookDao.getActiveBookByCategoryId(categorySingle._id, (bookResult) => {
if(bookResult) {
if(bookResult.length > 0) {
resultJson.book_count = bookResult.length;
}
}
});
categoryArray.push(resultJson);
}
callback(categoryArray);
}
});
}
bookDao.js
module.exports.getActiveBookByCategoryId = (categoryId, callback) => {
Book.find({
is_delete : false,
category : categoryId
}, (error, result) => {
if(error) {
console.log(error);
callback(commonUtil.ERROR);
}
if(result) {
callback(result);
}
});
}
Try this, In your code categoryArray.push(resultJson); will not wait for BookDao.getActiveBookByCategoryId to finish because of async behavior.
module.exports.getActiveBookByCategoryId = (categoryId) => {
return Book.count({
is_delete: false,
category: categoryId
});
}
module.exports.getAllActiveCategory = async () => {
try {
// Find all category
const result = await Category.find({
is_delete: false
});
// Create array of promise
const promises = result.map(categorySingle => BookDao.getActiveBookByCategoryId(categorySingle._id));
// Get array of Category count
const data = await Promise.all(promises);
// update count in result
return result.map((categorySingle, i) => {
categorySingle.book_count = data[i];
return categorySingle;
});
} catch (error) {
console.log(error);
}
}

Javascript function returning undefined from mongodb function

Hi I get data from mongoDB and I try insert new data from other function. For some reason I get undefined.
What I'm doing is wrong and how can I fix it?
router.post("/list", (req, res, next) => {
const params = req.body;
var results_data = [];
Countability.find()
.populate("user")
.populate("company")
.populate("store")
.exec(function (err, data) {
_.forEach(data, function (value, key) {
results_data[key] = {
_id: value._id,
count: GetCount(10)
};
});
res.status(200).json({ status: 1, result: results_data });
});
});
const GetCount = (id) => {
DataRow.aggregate(
[
{
"$group": {
"_id": "$rowName",
"count": { "$sum": 1 }
}
}
]).exec((err,res) => {
var data = Promise.all([res]);
data.then(function (res) {
return res
})
})
}
Though the forEach has not finished,server starts sending response.
You need to make sure the results_data has got all data,then call res.status(200).json

Categories