javascript hasOwnProperty() not finding property
-
21-12-2019 - |
Question
Working with angularjs and firebase writing a function that's supposed to check whether the currentUser can be found in the database. Right now the property "attendings" only contains one user and that is "Peter Pan"
services/party.js
isAttending: function (party) {
if (User.signedIn() && party.attendings) {
console.log(User.getCurrent().username); //Writes out: Peter Pan
console.log(eve.attendings); //Writes out: Peter Pan
console.log(party.attendings.hasOwnProperty(
User.getCurrent().username); //Writes out: false
return party.attendings.hasOwnProperty(User.getCurrent().username);
}
}
Peter Pan was stored when the party object was created:
var Party = {
..
create: function (p) {
...
party.attendings = user.username;
...
}
}
The user is apperantly stored in 'attendings' but why won't hasOwnProperty() find it?
Solution
I think you are mistaken about how hasOwnProperty works.
hasOwnProperty will return if the object has a property, in your case, party has a property attendings for instance, but that is all it will check, it won't check what the value of this property is (in this case Peter Pan).
Now, I assume you'd like attendings to be a list, in that case you need to find a value in a list, there are several ways, if you can use jQuery you could do something like:
$.inArray( User.getCurrent().username, [ party.attendings ] );
If you don't then take a look at different alternatives just googling to find a value in an array in javascript.