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.

7
  • possible duplicate of How do you check if a variable is an array in JavaScript? Commented Dec 8, 2014 at 12:34
  • People need to stop answering this question and voting to close it instead Commented Dec 8, 2014 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 Commented Dec 8, 2014 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 Commented Dec 8, 2014 at 13:16
  • Thanks, I've done that now. I'd appreciate if you removed the suggestion to close it Commented Dec 8, 2014 at 13:22

4 Answers 4

2

I ended up using

if (results.length === 0) {

Somehow this just worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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?
1

To check an object is array

 Object.prototype.toString.call(results) === '[object Array]'

7 Comments

I just tested it, it doesn't work... The else statements is still getting called.
@artooras Here's a question: is results really an array? Or is it an array-like object?
Good question - the documentation for this function specifies return value as list 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
Try console.log(Object.prototype.toString.call(results)) and see what that outputs
can you tell me what is the output of Object.prototype.toString.call(results)?
|
0

Try this.

 if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
   alert( 'Array!' );
 }else{
   alert( 'object!' );
 }

2 Comments

I just tested it, it doesn't work... The else statements is still getting called.
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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.