.deny Meteor use.. can't get it to work - javascript

I'm trying to deny a question to be submitted if it has a length of 0. But I don't quite understand Meteor deny.
Here's what's going on.
I am updating the question. It is currently set at "yes"
I update it to "yessir"
I console log it as follows:
Questions.deny({
update: function(userId, question) {
console.log(question.question.length);
}
});
but the result is 3. It seems to console log the field being updated, not what I am updating it TO.
This is a problem because how can I check the length of an input if this thing won't check it when it's being submitted.
Can someone enlighten me?

Have a look at the docs and you'll see that the 2nd argument to update is doc:
doc is the current version of the document from the database, without the proposed update
The only way to validate the length of question is to look at the 4th argument - modifier. The problem with this approach is that you must check the modifier for every possible way it could be modified. Fundamentally, this is why allow/deny is really hard to implement in all but the most simple cases.
Instead, I'd strongly suggest either using collection2 to enforce your schema or using methods to modify your documents.
Recommended reading:
Meteor method vs. deny/allow rules
Allow & Deny: A Security Primer

Collection.deny Function either returns true or flase.
If you want to deny update on certain criteria here goes your code like this
Questions.deny({
update: function(userId, question, fields, modifier) {
// check for critera
if(fields.question.length < 0)
return true // denys update for question length less than 0
else
return false // deny = false means allow = true
}
});

Related

arangojs: keepNull not an option for collection.save?

I'm going through the documentation for arangojs and looking at the function collection.update(), keepNull is one of the options that can be added. https://github.com/arangodb/arangojs/blob/master/docs/Drivers/JS/Reference/Collection/DocumentManipulation.md
When going through the same documentation for the function collection.save() (https://github.com/arangodb/arangojs/blob/master/docs/Drivers/JS/Reference/Collection/DocumentCollection.md) we find no such option. Why? Do I first need to have an original file, then update that one with keepNull: false before I get it to clean up my documents from any null valued keys? Or is this a lack in the documentation? I think it's correct since I haven't managed to set keepNull to false using collection.save myself.
The driver hands the query options over to the server, so this is the relevant documentation to look at:
https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#create-document
The API does not support keepNull as option when creating a document. It is only available for UPDATE/REPLACE queries to mark attributes for removal. So it's up to you to do this on the client-side. You may open a feature request nonetheless.
BTW. In AQL, UPDATE doc WITH {} OPTIONS { keepNull: false } will not remove any attributes with a null value! It only removes attributes you set explicitly to null in the WITH {} part. This may apply to the driver as well.

Optional parameters in Meteor router

I have a path in Meteor
path: '/my-url/:groupId?',
I have added the question mark to indicate that sometimes groupId is not used. In data I check whether the group id is set with this.params.hasOwnProperty('groupId') but I have come across that sometimes the router thinks groupId is set even if it isn't (don't know why) but the value is undefined.
Therefore, I tried
console.log(this.params.hasOwnProperty('groupId'));
console.log('groupId' in this.params);
console.log(this.params.groupId);
and they evaluate to
> true
> true
> undefined
So I guess hasOwnProperty is not the best way to check whether groupId is set since it doesn't check for undefined values.
What will be a better way to check this? Why does hasOwnProperty evaluate to true even if my url is /my-url?
I think you've already answered yourself on your question. I have same problem and my check is:
if (this.params.groupId) {
} else {
}
This is not yet confirmed, but I think as long as you provide :groupId, you're already have that in params, regardless it exists or not.

MongoDB determine if a record would pass findOne

I would like to find out how to do the below, without actually doing the DB Query.. i.e. I would like to know if "someData" would pass "whereClause" without putting it in a table and asking for it back again. I.e. Run the logic inside findOne without the overhead of insert and select. Just to make it more fun, please consider that I need to have it thread safe, thats why im messing with a Guid kinda thing below.. Also please note the where clause is probably gonna be more complex than the below, like { a : { $ne : 1 } }
Given Source:
someData = { a: 1, b: 2 };
whereClause = { b: 2 };
My code that needs fixing:
someData.GUID = ObjectId();
// DB QUERY - insert
db.workspace.insert(someData);
whereClause.GUID = inputsValues.GUID;
// Check if the data passes the whereClause
// DB QUERY - findOne
var whereResult = db.workspace.findOne(whereClause);
// DB QUERY - remove
db.workspace.remove({ "GUID": whereClause.GUID });
if (whereResult == null)
alert("Fail");
else
alert("Pass");
In SQL what I want can be expressed kinda like this: (pseudo syntax)
if (
Select Count(*) from ((Select 1 as A, 2 as B) Data where B = 2) Result
) = 1 then 'pass' else 'fail'
The above query never actually touches a table - that is my main goal.
Ok so I took this question to MongoDB support (10gen) and asked them for advice. It seems there is no syntactic way of saying what I want to say. Their suggestion is to use a separate mongodb instance as close to the application as possible, and to use that for only this purpose avoiding any potential slowing down due to locking etc.
So the solution is: Make a new MongoDB instance local to the app, and run thses queries in there. That would be the only way to do this.
I'll leave the question open to anyone that can provide a better solution.
Ok, so Stennie (in a comment) above has provided clues to what I think is the best answer for this situation.
He provided the link below:
what Javascript library can evaluate MongoDB-like query predicates against an object?
This has led me to 2 javascript libraries:
1) Mingo - https://github.com/kofrasa/mingo
2) Sift - https://github.com/crcn/sift.js
Mingo has the advantage of being able to evaluate MongoDB syntax, where Sift seems to be more complete. For my situation Mingo is perfect and exactly what I was looking for.
(I don't know how to give Stennie credit for this answer, but it really belongs to him)

AngularJS - Continuosly watch over a collection

I'm trying to build a real watcher for a collection in my app and, at first, I thought that Angular would provide me everything I needed.
I mean, I had the $watch, both shallow and deep. and the $watchCollection, a $digest cycle that loops over my $scope-exposed variables through the dirty checking mechanic and triggers all the watchers...
Great! What else could I need?
Wrong!
Turns out that $watchCollection gets triggered only at the first change of the watched variable...
And that's it for the mighty watchers... why???
After a reality check, I realized that I needed some kind of horrible loop to check this collection, or else I had to implement some sort of callback to do this, whenever the var gets modified.
Anybody knows how this can be done in the cleanest way possible?
Important note:
I don't why, but it seems that some horrific bug in my code was gnawing my ankles...
Now that I've fixed it, both $watchCollection(expr, foo) and $watch(expr, foo, true) works as expected...
I was mislead by this SO post , in which an user comments:
[...] I don't see anything in your code that makes the subsequent requests (to check for new messages). Where does that happen?
I took his comments as proof of my hypothesis... my bad!
I'm leaving this question as a memento
I'm pretty sure a regular $watch will do this if you utilize the 3rd parameter (objectEquality). This will check if the objects are equal and not just references.
So, you can use something like this:
$scope.$watch('prop', function(value) {
// do something
}, true);
The true value tells Angular to compare objects instead of references.
The documentation for this feature is with scope.
below solution is bit of an hacking solution and should only be used if $watchCollection does not work. rather than watching on the array, watch on json
$scope.$watch(function() {
return angular.toJson($scope.array);
},
function() {
// watch logic
}
I am using above solution to watch on multiple arrays like below:
$scope.$watch(function() {
return JSON.stringify([$scope.array1, $scope.array2]);
},
function() {
// watch logic
}
you can user either of JSON.stringify or angular.toJson.

What does the jQuery function $('#myelement').is('*') do?

What does the following code do:
$('#myelement').is('*')
What does the asterisk signify? Since there is only one element, #myelement, I can't understand the point of using is(), which checks if an element matches a set of elements?
This is some seriously existential JavaScript.
$('#myelement').is('*')
It will fail whenever #myelement doesn't exist, and return true otherwise.
Basically check to see if an element exists or not. Not the best method...
is checks the element fits the criteria. In this case, "*" means all elements.
So, it simply returns true if the previous selector returns anything.
Take a look here for an example: http://jsfiddle.net/b7DwB/
http://api.jquery.com/is/
Pretty much what it does well from my understanding of it at least, and how I tend to use it. Is return true or false on whatever its called on.
Example I have a checkbox that I want to make sure is checked before I submit my form via AJAX I would do something like
if( $('input#tosCheck').is(':checked') ){
/*its checked submit form*/
}else{
alert('Error');
}
All in all the link to the API from jQuery better describes it then I ever could, but I wanted to at least share an example of use to help you gauge some idea.
Can't say I've ever seen that jQuery code used before, but it seems to be a poor way of checking for the existence of an element. Since * is the universal selector, the expression in question will always return true if #myelement exists, otherwise it will return false.
I say this is a "poor" way of checking the existence of an element because you can simply check the length of the jQuery object instead:
$('#myelement').length > 0
I haven't done any testing, but I assume the above is faster since it doesn't have the overhead of the is() function call.

Categories

Resources