Could someone explain how this is happening? An array from Express is being destroyed before it is returned. In the example below, I am successfully populating the resData.user.apps array and can see this via the server's console log. However, when the resData object is returned the array is empty:
// Build the response
var resData = {
success: true,
user: {
email: newUserItem.email,
apps: []
}
};
// Add any default applications
Application.find({ isDefault: true, isDeleted: false }, function(err, defaultApps){
if (err){
console.log(err);
return res.status(500).send({ message: 'Failure loading default applications.' });
} else {
if (defaultApps.length < 1){
console.log('No default apps');
} else {
defaultApps.forEach(function(defaultApp){
var app = {
name: defaultApp.formalName,
url: defaultApp.url,
def: true
};
console.log(app);
resData.user.apps.push(app);
console.log('Sanity: ' + JSON.stringify(resData));
});
}
}
});
return res.send(resData);
The problem here is that the find is async so you are writing back the response before the call has completed. Move return res.send(resData); into the find callback.
Application.find({ isDefault: true, isDeleted: false }, function(err, defaultApps){
if (err){
console.log(err);
return res.status(500).send({ message: 'Failure loading default applications.' });
} else {
if (defaultApps.length < 1){
console.log('No default apps');
} else {
defaultApps.forEach(function(defaultApp){
var app = {
name: defaultApp.formalName,
url: defaultApp.url,
def: true
};
console.log(app);
resData.user.apps.push(app);
console.log('Sanity: ' + JSON.stringify(resData));
});
}
res.send(resData);
}
});
Related
Below is controller.js script. I am facing hard time to go inside if loop "if(changeUserToSellerFlag == 1)" because this if statement is executed much earlier than this if statement "if (results.Name == "Customer")". I am not sure why the order of execution is not done line by line.
const { updateUserSellerStatus } = require("./seller.model.js");
const { genSaltSync, hashSync } = require("bcrypt");
const userid = require("../auth/token_validation.js");
const {
updateUserRoleAfterSellerApproval,
getRoleName,
} = require("../users/user.service.js");
module.exports = {
sellerVerify: (req, res) => {
var sellerStatusName;
var changeUserToSellerFlag = 0; // this flag determines if user to seller update is needed or not
approvedByName = req.fullName; //get full name from token_validation.js
approvedByEmail = req.email; //get email from token_validation.js
console.log("Request body is below");
console.log(req.body); // contains body sent by postman
updateUserSellerStatus(
req.body,
approvedByName,
approvedByEmail,
(err, results) => {
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
if (!results) {
console.log(results);
return res.json({
success: 0,
message: err,
});
}
if (req.body.sellerStatus == 3) {
sellerStatusName = "Rejected";
} else if (req.body.sellerStatus == 2) {
sellerStatusName = "Completed";
} else if (req.body.sellerStatus == 1) {
sellerStatusName = "Pending";
}
//verify if user is a seller or not
if (req.userId) {
getRoleName(req.userId, (err, results) => {
console.log("check rolename gettign below");
console.log(results);
if (results.Name == "Customer") {
console.log("User is Customer");
changeUserToSellerFlag = 1;
console.log("flag is :" + changeUserToSellerFlag);
} else if (results.Name == "Seller") {
console.log("User is Seller");
changeUserToSellerFlag = 0;
}
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
});
}
console.log("see flag changed here ", changeUserToSellerFlag);
if (changeUserToSellerFlag == 1) {
console.log("i am here");
//update userrole to seller only if user is not a seller earlier
updateUserRoleAfterSellerApproval(
req.userId,
req.roleId,
(err, results) => {
console.log("result details are bwlo");
console.log(results);
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
if (!results) {
console.log(results);
return res.json({
success: 0,
message: err,
});
}
console.log("Update into UserRole Completed successfully");
},
);
} else {
console.log(
"User is already a seller and dont need to update userrole table",
);
}
console.log(sellerStatusName);
return res.status(200).json({
success: 1,
status: sellerStatusName,
});
},
);
},
};
Below is console output
Request body is below
{ sellerId: 122, sellerStatus: 2 }
see flag changed here 0
User is already a seller and dont need to update userrole table
Completed
check rolename gettign below
TextRow { Name: 'Customer', RoleId: 2 }
User is Customer
flag is :1
where "User is already a seller and dont need to update userrole table" is executed first before "flag is :1"
to me it looks as if you're treating asynchronous code as if it was synchronous. At the top of the function you are calling parts of the code that are asynchronous, like for example every database query is generally async call.
getRoleName(req.userId, (err, results) //i assume this is an async call
process will not wait for the above code to finish before proceeding forward with its execution because it is written synchronously. So next instructions that it will execute are:
console.log('see flag changed here ', changeUserToSellerFlag);//0
if(changeUserToSellerFlag == 1)//0 --> false
}else{console.log("User is already a seller and dont need to update userrole table");}//so these 3 orders are executed immediately.
after which the async call from (i assume)
getRoleName(req.userId, (err, results)
will have finished and the proper callback will be called.
as a potential solution:
I would either wait for any database call to finish before proceeding. You can use async - await call, other solutions are also usage of Promises and of course making a callback hell.
If we use callback because you are already using them it would look something like this:
updateUserSellerStatus(
req.body,
approvedByName,
approvedByEmail,
(err, results) => {
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
if (!results) {
console.log(results);
return res.json({
success: 0,
message: err,
});
}
if (req.body.sellerStatus == 3) {
sellerStatusName = "Rejected";
} else if (req.body.sellerStatus == 2) {
sellerStatusName = "Completed";
} else if (req.body.sellerStatus == 1) {
sellerStatusName = "Pending";
}
//verify if user is a seller or not
if (req.userId) {
getRoleName(req.userId, (err, results) => {
console.log('check rolename gettign below');
console.log(results);
if (results.Name == "Customer") {
console.log("User is Customer");
changeUserToSellerFlag = 1;
console.log("flag is :" + changeUserToSellerFlag);
} else if (results.Name == "Seller") {
console.log("User is Seller");
changeUserToSellerFlag = 0;
}
console.log('see flag changed here ', changeUserToSellerFlag);
if (changeUserToSellerFlag == 1) {
console.log("i am here");
//update userrole to seller only if user is not a seller earlier
updateUserRoleAfterSellerApproval(
req.userId,
req.roleId,
(err, results) => {
console.log("result details are bwlo");
console.log(results);
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
if (!results) {
console.log(results);
return res.json({
success: 0,
message: err,
});
}
console.log("Update into UserRole Completed successfully");
}
);
} else {
console.log("User is already a seller and dont need to update userrole table");
}
console.log(sellerStatusName);
return res.status(200).json({
success: 1,
status: sellerStatusName,
});
}
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: err,
});
}
});
}
);
},
};
I just moved the code which was below the
console.log('see flag changed here ', changeUserToSellerFlag);
inside the first callback method.
I'd recommend refactoring the callback-hell asynchronicity to promises.
Happily, util.promisify in Node's util library makes it super easy to do that for functions that are callback-async.
You end up with something like this:
const { promisify } = require("util");
const { updateUserSellerStatus } = require("./seller.model.js");
const userid = require("../auth/token_validation.js");
const {
updateUserRoleAfterSellerApproval,
getRoleName,
} = require("../users/user.service.js");
const updateUserSellerStatusP = promisify(updateUserSellerStatus);
const getRoleNameP = promisify(getRoleName);
const updateUserRoleAfterSellerApprovalP = promisify(
updateUserRoleAfterSellerApproval,
);
async function sellerVerify(req) {
var sellerStatusName;
var changeUserToSellerFlag = 0; // this flag determines if user to seller update is needed or not
switch (req.body.sellerStatus) {
case 3:
sellerStatusName = "Rejected";
break;
case 2:
sellerStatusName = "Completed";
break;
case 1:
sellerStatusName = "Pending";
break;
}
const results = await updateUserSellerStatusP(
req.body,
req.fullName,
req.email,
);
if (!results) throw new Error("No results from UUSS");
//verify if user is a seller or not
if (req.userId) {
const roleName = await getRoleNameP(req.userId);
if (roleName.Name === "Customer") {
changeUserToSellerFlag = 1;
} else if (roleName.Name === "Seller") {
changeUserToSellerFlag = 0;
}
}
if (changeUserToSellerFlag) {
console.log("i am here");
//update userrole to seller only if user is not a seller earlier
const results = await updateUserRoleAfterSellerApprovalP(
req.userId,
req.roleId,
);
console.log(results);
if (!results) throw new Error("UURASP results missing");
console.log("Update into UserRole Completed successfully");
} else {
console.log(
"User is already a seller and dont need to update userrole table",
);
}
console.log(sellerStatusName);
return {
success: 1,
status: sellerStatusName,
};
}
module.exports = {
sellerVerify: async (req, res) => {
try {
const result = await sellerVerify(req);
return res.json(result);
} catch (err) {
console.error(err);
return res.status(500).json({
success: 0,
message: err,
});
}
},
};
I'm using the loopback for backend API, here for fetching the data from MySQL, here while performing the operation on it I am using the async npm library,
to perform block-level functional execution, while doing these facing setback with an autocallBack functional issue.
Unhandled rejection TypeError: autoCallback is not a function
here is my pseudo code.
ModalName.remoteMethod = function (data, cb) {
async.auto({
firstCallingFunction: function (autoCallback) {
ModalName.find({
id: 1
}, yourResult => {
if (err) {
return cb({
success: false,
msg: 'Insufficient parameters',
data: err,
});
} else {
return autoCallback(null, yourResult);
}
});
},
secondCallingFunction: ['firstCallingFunction', function (autoCallback, result) {
console.log('result=====>', result)
ModalName.find({id: result['id']})
.then(function(dbResult) {
if (dbResult) {
console.log('dbResult==========>', dbResult.toJSON());
return autoCallback(null, dbResult);
}
});
}],
}, function (error, autoResult) {
if (error) {
return cb(null, error);
} else {
return cb(null, {
success: true,
msg: 'result fetched',
data: autoResult.secondCallingFunction,
});
}
});
};
Get in ==> err in autoResult's object function is undefined.
Any solution for that issue, please send.
Thanks.
Here is the solution, I just changed the function arguments and it works for me!
ModalName.remoteMethod = function (data, cb) {
async.auto({
firstCallingFunction: function (autoCallback) {
ModalName.find({
id: 1
}, yourResult => {
if (err) {
return autoCallback({
success: false,
msg: 'Insufficient parameters',
data: err,
});
} else {
return autoCallback(null, yourResult);
}
});
},
secondCallingFunction: ['firstCallingFunction', function (result, autoCallback) {
console.log('result=====>', result)
ModalName.find({id: result['id']})
.then(function(dbResult) {
if (dbResult) {
console.log('dbResult==========>', dbResult.toJSON());
return autoCallback(null, dbResult);
}
});
}],
}, function (error, autoResult) {
if (error) {
return cb(null, error);
} else {
return cb(null, {
success: true,
msg: 'result fetched',
data: autoResult.secondCallingFunction,
});
}
});
};
I send data from my input fields to my api:
$.ajax({
url: '/api/login',
type: 'GET',
dataType: 'json',
ContentType: 'application/json',
data: {formData},
success: (data) => {
console.log('SUCCESS')
console.log(data)
this.setState({
isInProcess: false
})
},
error: (jqXHR) => {
console.log(jqXHR)
console.log('ERROR')
this.setState({isInProcess: false})
}
})
on my server-side I have a function to see if I have required user in db:
async function findUser(data) {
try {
const user = await User.findOne({email: data.email,
password: data.password})
console.log('User was found')
return { user }
} catch (err) {
console.log('error', err)
throw err
}
}
which will be executed here:
app.get('/api/login', async (req, res) => {
const data = req.query
try {
const foundUserData = await findUser(data.formData)
return res.json(foundUserData)
} catch (err) {
return res.status(400).json(err)
}
})
It works fine, but if a user wasn't found in db i sends success anyway. Why?
await findUser(data.formData) won't throw error, return either null or user object. You may check something following
app.get('/api/login', async (req, res) => {
const data = req.query
try {
const foundUserData = await findUser(data.formData)
if(foundUserData && foundUserData.user) {
return res.json(foundUserData)
} else {
return res.status(400).json({message: 'User not found'});
}
} catch (err) {
return res.status(500).json(err)
}
})
It sends success because none of your queries error'ed, just because it didn't find anything does not mean that the query failed because it obviously succeeded in finding out if what ever you're looking for exists or not.
To send an error in case of not found you need to check if response is empty in which case you want to send error
When no user is find you get a null value. You may try to put more logic on your success parameter with that for example:
success: function (data) {
if(!!data && data != null) {
alert('Success');
} else {
alert('No data');
}
}
In console it shows response but in postman shows sending request like that only. How can I return a valid response in postman. How can I write code for this one i tried all.
This is my code so far:
var sendStreamingTemplate = function (req, res) {
authToken = req.headers.authorization;
userAuthObj = JSON.parse(UserAuthServices.userAuthTokenValidator(authToken));
var todayDate = new Date();
var expireDate = new Date(userAuthObj.expire_date);
tokenOK = TokenValidator.validateToken(userAuthObj.user_id, authToken).then(function (userSessions) {
if (userSessions.length === 1) {
if (expireDate >= todayDate) {
StreamingTemplateId = req.params.id;
Template.findById(StreamingTemplateId).then(function (streamingTemplate) {
if (streamingTemplate === null) {
res.status(404).json({
message: 'Streaming not found...'
})
} else {
console.log(streamingTemplate);
switch(streamingTemplate.template_name.toString().toLowerCase()){
case "notification":
//if write return response means it will return something went wrong
break;
case "invoice":
break;
case "voucher":
break;
default:
break;
}
}
}).catch(function (err) {
res.status(500).json({
message: 'something went wrong...'
});
});
} else {
res.status(401).json({
message: 'Not Authorized...'
});
}
} else {
res.status(401).json({
message: 'Token Expired...'
});
}
}).catch(function (err) {
res.status(401).json({
message: 'Token Expired...'
});
});
};
This is the the output in the console:
Instance {
dataValues: {
id: 1, template_name: 'StreamNotification', description: 'Streaming', template_content: 'Mail notification', is_active: true
},
_previousDataValues: {
id: 1, template_name: 'StreamNotification', description: 'Streaming', template_content: 'Mail notification', is_active: true
},
_changed: {
},
}
It looks like when you hit the switch statement the code isn't setting the response. Try adding this immediately after your console.log line:
res.json(streamingTemplate)
or even
res.send(streamingTemplate)
depending on the type of streamingTemplate. You might need to vary the response depending on the switch statement, in which case you could set some return value in each case then move the response to the end of the switch. Depends what you are trying to do.
I am using Parse Server on AWS and mLab with great success, except for my Cloud Code. The main issue is surrounding my previous code for Create OR Update an object. I used to do this by querying for a user pointer on the Favourites class. If a row contains a user pointer then I need to update its content, if it doesn't exist a row needs to be created.
Old Parse.com Code
Parse.Cloud.define("saveFavourites", function(request, response) {
console.log(request.params.favourites);
var Favourites = Parse.Object.extend("Favourites");
var query = new Parse.Query("Favourites");
query.equalTo('user', request.user);
query.first({
success: function(results) {
console.log(JSON.stringify(results));
console.log(results)
if (results === undefined) {
var favourites = new Favourites();
favourites.save({
user: request.user,
favourites: request.params.favourites
}, {
success: function(favourites) {
// The object was saved successfully.
},
error: function(favourites, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
}
});
} else {
results.set("favourites", request.params.favourites);
results.set("userId", request.user.id);
results.save();
}
response.success(results);
},
error: function(error) {
error.message("favourites lookup failed");
}
});
});
New Parse Server Code
Parse.Cloud.define("saveFavourites", function(request, response) {
console.log('user is : ' + JSON.stringify(request.user));
var Favourites = Parse.Object.extend("Favourites");
var query = new Parse.Query("Favourites");
query.equalTo("user", request.user);
query.first({
useMasterKey: true
}, {
success: function(results) {
if (results && results.length > 0) {
console.log('running found');
favourites.set("favourites", request.params.favourites);
favourites.set("userId", request.user.id);
favourites.save();
response.success();
} else {
var favourites = new Favourites();
favourites.set("user", request.user);
favourites.set("favourites", request.params.favourites);
favourites.set("userId", request.user.id);
favourites.save();
response.success();
}
},
error: function(error) {
console.log(error.message);
}
});
});
Do not response unless callback finished. Set response.error on each Parse requests error.
Parse.Cloud.define("saveFavourites", function(request, response) {
console.log(request.params.favourites);
var Favourites = Parse.Object.extend("Favourites");
var query = new Parse.Query("Favourites");
query.equalTo('user', request.user);
query.first({
//is this query need masterKey?
useMasterKey: true,
success: function(results) {
console.log(JSON.stringify(results));
console.log(results)
if (results === undefined) {
var favourites = new Favourites();
favourites.save({
user: request.user,
favourites: request.params.favourites
}, {
success: function(favourites) {
// The object was saved successfully.
response.success(results);
},
error: function(favourites, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
response.error(error);
}
});
} else {
results.set("favourites", request.params.favourites);
results.set("userId", request.user.id);
results.save(null, { useMasterKey: true }).then(response.success, response.error);
}
},
error: function(error) {
error.message("favourites lookup failed");
response.error(error);
}
});
});