0

I have this code running on Parse.com CloudCode

queryContact.find().then(function(results) {

    console.log(typeof results); // object

    if (results.constructor !== Array) {

        response.success("Found zero results");

    } else {

        console.log("WHY DID IT GO THROUGH!!!");
    }

}).then...

The find() function normally returns an array, but in my test case it returns 0 results. By logging to console I managed to see that in this case results is a typeof object. I want to proceed with else case only if results is typeof Array. However, my checks fail to capture this, and the code keeps falling through into the else section. None of the checks from this SO question work for me.

artooras
  • 6,315
  • 9
  • 45
  • 78
  • possible duplicate of [How do you check if a variable is an array in JavaScript?](http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript) – Dexygen Dec 08 '14 at 12:34
  • People need to stop answering this question and voting to close it instead – Dexygen Dec 08 '14 at 12:35
  • Thanks for you diligence George, but I have quoted the same SO question in my question - see bottom. Hence my question here. I was hoping someone here would have experience with Parse's find() function and CloudCode in case they cause some specific behaviours – artooras Dec 08 '14 at 12:58
  • Please name your question better then, if it's not generally about "Checking if object is Array" -- that ground has been covered umpteen times before – Dexygen Dec 08 '14 at 13:16
  • Thanks, I've done that now. I'd appreciate if you removed the suggestion to close it – artooras Dec 08 '14 at 13:22
  • Are you sure `results` can really be an array, and not only array-like object? If it's an array, the answers in the other question should work. – Oriol Dec 08 '14 at 20:21
  • The documentation says the output is a list https://parse.com/docs/js/symbols/Parse.Query.html#find – artooras Dec 08 '14 at 20:44

4 Answers4

2

I ended up using

if (results.length === 0) {

Somehow this just worked for me.

artooras
  • 6,315
  • 9
  • 45
  • 78
  • This is the correct answer for this question, because Parse.com always returns the results array; all you have to do is check its length. – Seth Dec 08 '14 at 19:40
  • You may be right, but it's still strange the the returned array does not respond / allow itself to be identified as an Array. The documentation states the the method returns a list, not array. Can this be the difference? – artooras Dec 08 '14 at 19:59
1

To check an object is array

 Object.prototype.toString.call(results) === '[object Array]'
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • I just tested it, it doesn't work... The else statements is still getting called. – artooras Dec 08 '14 at 12:52
  • @artooras Here's a question: is `results` *really* an array? Or is it an array-like object? – CodingIntrigue Dec 08 '14 at 12:58
  • Good question - the documentation for this function specifies return value as list https://parse.com/docs/js/symbols/Parse.Query.html#find - I assumed it's an array because that's what I get through Parse's iOS API – artooras Dec 08 '14 at 13:02
  • Try `console.log(Object.prototype.toString.call(results))` and see what that outputs – CodingIntrigue Dec 08 '14 at 13:08
  • can you tell me what is the output of `Object.prototype.toString.call(results)`? – Fizer Khan Dec 08 '14 at 14:19
  • The console output is `[object Array]` – artooras Dec 09 '14 at 08:51
  • The console output is `[object Array]`, which makes your equation correct. I see the flaw in my original if statement as I was mislead by the result of `console.log(typeof results);` being `object` - the output is always an Array, even when there are 0 results – artooras Dec 09 '14 at 08:58
0

Try this.

 if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
   alert( 'Array!' );
 }else{
   alert( 'object!' );
 }
Amit Prajapati
  • 13,525
  • 8
  • 62
  • 84
  • I just tested it, it doesn't work... The else statements is still getting called. – artooras Dec 08 '14 at 12:53
  • Oh, I see. json response is not relevant as it contains a crash error that relates to code happening later in the else case, and which would be prevented if I can stop the else case to run altogether – artooras Dec 08 '14 at 13:21
0

You can use the following to return the names of JavaScript types:

function toType(x) {
  return ({}).toString.call(x).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}

toType([]); // array
toType({}); // object

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95