Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created January 14, 2014 23:09
Show Gist options
  • Save robwormald/8427779 to your computer and use it in GitHub Desktop.
Save robwormald/8427779 to your computer and use it in GitHub Desktop.
/**
* parseWhereParam
*
* @param {Object} allParams [result of calling req.params.all()]
* @return {Object} the WHERE criteria object
*/
function parseWhereParam( allParams ) {
var where = req.param('where');
// If `where` parameter is a string, try to interpret it as JSON
if (sails.util.isString(where)) {
where = tryToParseJSON(where);
}
// If `where` has not been specified, but other unbound parameter variables
// **ARE** specified, build the `where` option using them.
if (!where) {
// Prune params which aren't fit to be used as `where` criteria
// to build a proper where query
where = sails.util.omit(allParams, ['limit', 'skip', 'sort']);
//this is the generated method (broken)
// where = sails.util.omit(allParams, function (p){ if (sails.util.isUndefined(p)) return true; });
//this is the fixed method
where = sails.util.omit(where, function (p){ if (sails.util.isUndefined(p)) return true; });
if (isJSONPCompatible) { where = sails.util.omit(where, JSONP_CALLBACK_PARAM); }
}
return where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment