node.js token deletion promise - javascript

I have a delete route for my api that deletes tokens stored in our mongodb. I am currently writing a check that makes sure the token is not associated with another object before it is processed for deletion.
function checkTokenIsNotUsed(req) {
console.log('TEST ' + req.params.token);
objectDB.findObject('artefact', {"data.token": req.params.token})
.then(function(result) {
console.log('findObject result' + result);
if (isDefined(result)) {
console.log('Artefact Exists');
serverError(res, 'Cannot delete token as it is associated with an artefact');
} else {
console.log('Token not being used by an artefact, okay for deletion');
};
})
.catch(function(err){
console.error('Token CHECK DELETE error: '+err.message);
serverError(res, 'err.message');
});
return(result)
};
//
// Token deletion
//
app.delete('/:token', function(req, res, next) {
checkTokenIsNotUsed(req)
.then(function(results){
return tokenModel.findOne({token: req.params.token});
})
As it stands, when I hit the route the checkTokenIsNotUsed function is called, and prints 'TEST + <token>' to the console, and then kicks out to cannot read property 'then' of undefined.
I have checked my query in the mongo shell independently of the platform interface that I am using and the query works as expected. The api route also works as expected without the checkTokenIsUsed function active.
Any ideas as to why the promise for objectdb.findObject() is not returning a result? It is intended to return a list of objects or nothing at all, and does as expected in other locations in this file.
-findObject is properly exported + imported from its respective location
-the query is correctly structured and works in mongo shell.
-the token itself is being printed just before this function is run, so checkTokenIsNotUsed is getting called and being executed.
-I don't really care that the conditional is just printing to the console at the moment, as my objective right now is to merely enter into that part of the function.
EDIT added
findObject = function(objType, query, options) {
return new Promise(function(resolve,reject) {
var populateOptions;
var findQuery = {};
var findOptions = {
sort: 'metaData.createTS',
page: 1,
limit: 50
};
var objectDef = getObjectDef(objType);
if (!isDefined(objectDef)) return resolve(null);
// Make sure query exists and has objType set
if (isDefined(query)) findQuery = query;
if (!isDefined(findQuery.objType)) findQuery.objType = objType;
if (isDefined(options)) {
if (isDefined(options.sort)) findOptions.sort = options.sort;
if (isDefined(options.page)) findOptions.page = toInt(options.page);
if (isDefined(options.limit)) findOptions.limit = toInt(options.limit);
}
if (isDefined(objectDef.populate)) populateOptions = objectDef.populate;
else populateOptions = "";
objectDef.model.find(findQuery, null, findOptions)
.populate(populateOptions)
.exec(function(error, list) {
if (error) reject(error);
else resolve(list);
});
});
};

Related

Mongoose find search if statement error using expressjs

This is a URL shorten-er project
app.get('/:url', (req,res) => {
Url.find({userUrl:req.params.url},(err,doc)=>{ //finds if there is a link in the database
if(err){
console.error(err)
}else if(doc[0].userUrl==req.params.url) { // there is an error in this line if a new link is passed in the params.url, or else if the link existed then there is no issue
console.log('hi')
}else { // if no link is not reaching?
const url = new Url({
userUrl:req.params.url
})
url.save();
}
})
})
if i have a link in my database example google.com , it does give me output of hi , but when i put other link it gives me a 'property userUrl undefined' error in the first else if statement.
my schema is like this let urlsSchema = new Schema({
userUrl:String,
shortUrl: {
type: String,
'default': shortid.generate
}
})const Url = mongoose.model('urls',urlsSchema);`
i think i need to write it in a way where, if you cant find it in the database then create a new url document.. not working tho
`
The main problem is that find only throws an error if either the request is corrupted or the database connection does not work. If no data was found, it will not return an error but rather an empty doc array. If you then access doc[0], it will return undefined, and undefined has no userUrl. A possible solution would be to check if the doc array has a first document:
Url.find({ userUrl: req.params.url }, (err, doc) => {
if(err){
return console.error(err);
if(doc[0]){
//Exists already
console.log(doc[0]);
} else {
//Doesnt exist yet, so create a new one:
const url = new Url({
userUrl: req.params.url
})
url.save();
}
});
Note that you should rather use findOne to check for existence.

fetch a particular value using node js and mongodb

I have to try to fetch a field value from MongoDB using Node.js. But it shows me undefined in my console. My requirement is to print the data in the console or browser from MongoDB using Node.js.
1). This is my node js
this.levelChange = function(req, res, next){
try{
var query = {'level_num':2};
QuizLevel.find(query,function(err,data){
var a = data.min_score;
console.log(a);
res.send(a);
});
}catch(err){
console.log("Error");
return next(err);
}
};
2). This is my js-schema
{
_id:{type:String},
age:{type:Number},
level_num:{type:String},
min_score:{type:String},
max_questions:{type:String}
}
3).This is my console output
undefined
4). This is my JSON data
{
"age":5,
"level_num":1,
"min_score":10,
"max_questions":30
},
{
"age":5,
"level_num":2,
"min_score":12,
"max_questions":33
}
Simply use findOne(find return an array of document) with a project field(return only desired fields).
And don't forget to check the err field !
try{
var query = {'level_num':2};
QuizLevel.findOne(query,{min_score: 1}, function(err,data){
if(err || !data)
{
console.log(err);
return next(err);
}
else
{
var a = data.min_score;
console.log(a);
res.send(a);
}
});
}catch(err){
console.log("Error");
return next(err);
}
I might be incorrect but it looks like you're trying to access object property while the result is a collection, see:
data.min_score // => [{ ... }, { ... }].min_score
vs
data[0].min_score
What you want to achieve is something like:
var scores = data.map((function (item) {
return item.min_score;
});
console.log(scores);
You can always check the type of result with console.log(typeof data) or simply write console.log(data), sometimes console.log(Object.keys(data)) come in handy as well for simple debugging not to mention node-inspector.

Promise either never get called, or is rejected (Parse JS SDK)

I am trying to write a function that add or edit some fields on a User object.
The problem come when I try to save the user, if I use user.save, the Promise is rejected with error 206 UserCannotBeAlteredWithoutSessionError.
However, if I get the session id (and documentation about that is scarce), the promise never get resolve, nor rejected. The app seems to just jump to the callback.
My function:
function update(user, callback) {
let query = new Parse.Query(Parse.User);
query.equalTo("username", user.email);
query.find().then(
(users) => {
if(users.length === 0) {
callback('Non existent user');
} else {
let user = users[0];
// user.set('some', 'thing');
console.log('save');
user.save(/*{
sessionToken: user.getSessionToken()
}*/).then(
(test) => {
console.log('OK - ' + test);
callback();
}, (err) => {
console.log('ERR- ' + require('util').inspect(err));
// console.log(callback.toString());
callback(error.message);
}
);
}
},
(error) => {
callback(error.message);
}
);
}
Called with:
var async = require('async'),
baas = require('./baas.js');
async.waterfall([
(callback) => {
callback(null, {
email: 'user#test.com',
password: 'password'
});
},
(user, callback) => {
console.log('connect');
baas.connect(() => { //Initialize the connection to Parse, and declare use of masterKey
callback(null, user);
});
},
(user, callback) => {
console.log('update');
baas.update(user, (err) => {
callback(err);
});
}
], (err) => {
console.log('Error: ' + err);
});
The logs become:
Without session token:
connect
update
save
ERR- ParseError { code: 206, message: 'cannot modify user sA20iPbC1i' }
With session token:
connect
update
save
I do not understand how it is possible that the promise just callback without printing anything, nor why no error are raised anywhere.
Edit:
Following #user866762 advice, I tried to replace the query with Parse.User.logIn and use the resulting User object.
While this solution give me a sessionToken, the end result is the same, parse crash if I don t provide the session token, or give me a error if I do.
According to the Parse Dev guide:
...you are not able to invoke any of the save or delete methods unless the Parse.User was obtained using an authenticated method, like logIn or signUp.
You might also try becoming the user before saving, but I have my doubts that will work.
When you're "get[ting] the session id" my guess is that you're really breaking something. Either Parse is having a heart attack at you asking for the session token, or when you're passing it in save you're causing something there to explode.

NodeJS is asynchronous and my code doesn't run in the order I am expecting

postRegistrationHandler: function (account, req, res, next) {
console.log('postRegistrationHandler activated');
account.getCustomData(function(err, data) {
if (err) {
console.log(err.toString, "error string");
return next(err);
} else {
data.mongo_id = userCreationCtrl(account);
data.save();
next();
}
});
},
This function almost works properly, but the line:
data.save();
runs before the previous line finishes which means that the data I want to save isn't present at the appropriate time.
data.mongo_id = userCreationCtrl(account);
This line calls a function that creates a mongoDB document with information in the account object and then returns the _id (which is what I am trying to save.
I thought maybe using a .then() would help but that seems to be unavailable here for some reason. If anyone sees something I'm missing, that would be quite helpful. Thank you!
Here is the userCreationCtrl file as requested:
var UserSchema = require('./../models/UserModel.js');
var createNewUser = function (account, res, next){
// We will return mongoId after it is created by submitting a newUser
var mongoId = "";
// Save StormpathID (last 22 characters of account.href property)
var newStormpathId = account.href.slice(account.href.length - 22);
console.log('stormpath ID:', newStormpathId, 'just registered!');
console.log(account);
// Create new user from model by recycling info from the Stormpath registration form and include the stormpathId as well.
var newUser = new UserSchema({
stormpathId: newStormpathId,
firstName: account.givenName,
lastName: account.surname,
email: account.email,
street: account.street,
city: account.city,
zip: account.zip
});
// This saves the user we just created in MongoDB
newUser.save(function(err, result){
console.log(result);
if (err) {
console.error(err);
}
else {
console.log("User created in MongoDB, attempting to return mongoDB _id to stormpath customData");
// Keep track of the new user's mongo _id so we can return it to the previous function and save it as Stormpath custom data.
mongoId = result._id;
console.log(mongoId, "mongoid");
return result._id;
}
});
};
module.exports = createNewUser;
You have userCreationCtrl expecting 3 arguments, account, res, and next. next is the callback that should be called after the user is created so instead of return result._id you should call next like so:
// inside of createNewUser()
newUser.save(function(err, result){
console.log(result);
if (err) {
console.error(err);
}
else {
console.log("User created in MongoDB, attempting to return mongoDB _id to stormpath customData");
// Keep track of the new user's mongo _id so we can return it to the previous function and save it as Stormpath custom data.
mongoId = result._id;
console.log(mongoId, "mongoid");
// IMPORTANT change to make it all work...
// get rid of return result._id because its not doing anything
// pass the value to your callback function instead of returning the value
next(null, result._id);
}
});
then calling code in postRegistrationHandler should look like this:
account.getCustomData(function(err, data) {
if (err) {
console.log(err.toString, "error string");
return next(err);
} else {
// pass in a callback as the 3rd parameter that will be called by newUser.save() when its finished
userCreationCtrl(account, null, function(err, resultId) {
data.save();
next();
});
}
});

Returning a response immediately with res.json

I have an express route which takes in some parameters, queries the database, and then returns some response.
I am using sequelize to query the db:
router.get('/query', function(req,res) {
var name = req.params.name;
var gid = req.params.gid;
// Query the db
models.user.find({ where: { name: name }}).then(function(user) {
models.group.find({ where: { id: gid }}).then(function(group) {
// if user found, return data to client
if (user) {
res.json({"user": user, "group": group});
}
});
}).catch(function(error) {
// catch any errors from db query
res.status(500).json({"error":error});
});
// Return a server error for any other reason
// This causes ERROR
res.status(500).json({"error":"Something went wrong. Check your input."});
});
But I keep getting the error on the last line:
Can't set headers after they are sent
It seems like the last line is ALWAYS run, even if it finds a user (which should return data to the client and be done).
Why doesn't res.json(..) immediately return to the client when a user is found? Since headers were already set, when the last line runs, it throws that error.
You need to only conditionally return an error. The line:
res.status(500).json({"error":"Something went wrong. Check your input."});
is always getting executed. The reason for this is that the function you pass to the find method is only called later in the event loop after the db responds. This means that when that call back is called you have already set the error on the response.
Your should either remove that line or decide when you want to return an error but don't return an error every time.
Remember javascript is asynchronous.
As soon you call this function
models.user.find({ where: { name: name }})
That last line is executed:
res.status(500).json({"error":"Something went wrong. Check your input."});
It seems you are trying to cater for 2 scenarios:
Bad request data from client - i.e. no gid given
Internal server errors - i.e. error with the database
I would recommend changing your catch function to something like this:
.catch(function(error) {
// catch any errors from db query
if (err === "Unable to connect to database") {
return res.status(500).json({ error: "There was an internal error"})
}
res.status(400).json({"error": "Bad input, please ensure you sent all required data" });
});
Have a read up on the list of standard HTTP status codes:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
What #bhspencer said is right. You have to remove that last line.
That line probably gets executed before any query in the database.
You need to implement a return in
models.user.find({ where: { name: name }}).then(function(user) {
models.group.find({ where: { id: gid }}).then(function(group) {
// if user found, return data to client
if (user) {
res.json({"user": user, "group": group});
return;
}
});
}).catch(function(error) {
// catch any errors from db query
res.status(500).json({"error":error});
return;
});
Actually res.json( does not end the processing of node.js code execution without return statement.

Categories

Resources