0

UPDATE.

I've now added a screen shot of the parse data browser showing a row using customer number 21.

enter image description here

UPDATE.

Based on the comments below and the suggested answer I have updated my code. It functions without error but does not appear to have the desired result.

I see two issues.

Once, I don't see anything being stored in the cname or cnumber variables, they appear undefined.

Secondly, I wanted the result of the user typing in a number (searchnumber) into the input box that matches that stored (cnumber) in parse to then return this parse object to the user.


Whats wrong with this code? it runs in the browser without errors in Chrome dev tools, but does not appear to return the "CustomerObject" If I run it outside of the function and without the search button it will work, be it only return an empty object.

Is there a problem with my code structure?

HTML

  <input type="text" name="searchnumber" id="searchnumber" value="" placeholder="Customer Number"/>  
  <button type="submit" onclick = "search()" >Find</button>

JS

function search() {
    var CustomerObject = Parse.Object.extend("CustomerObject");
    var retrieve = new Parse.Query(CustomerObject);  
    retrieve.equalTo("customernumber", $('#searchnumber').val()); 
            retrieve.first({ 
                success: function(retrieveResults) 
            {

            }

        });
            var cname = retrieve.get("customername");  
            var cnumber = retrieve.get("customernumber");

      }; 
6
  • 4
    Looks like you have not taken into account that this is an asynchronous method … Commented May 7, 2014 at 16:35
  • in your function never return object, you should have define your object before return for can return like this funcion retrieve(){ var temp={"id":1,"name":"hello"}; return temp;}, i dont know if this action do you want to done Commented May 7, 2014 at 16:59
  • The query runs, you're just not doing anything with the results. You need to do all post-processing inside the success handler, accessing retrieveResults and calling other methods from there. Commented May 7, 2014 at 17:08
  • If someone can provide the code example in the answer I'm happy to accept that and the feedback in the comments. Commented May 7, 2014 at 17:23
  • Please also show the code where you are using the values of cname and cnumber as you will need to have all that code inside the success function. Commented May 8, 2014 at 12:57

1 Answer 1

1

Inside the success function, do this:

var cname = retrieveResults.get("customername");
var cnumber = retrieveResults.get("customernumber");

You should rename retrieveResults to object or customerObject or something.

You're trying to get customername and number from the query, not the retrieved object.

Full code:

function search() {
    var CustomerObject = Parse.Object.extend("CustomerObject");
    var retrieve = new Parse.Query(CustomerObject);
    retrieve.equalTo("customernumber", $('#searchnumber').val());
    retrieve.first({
        success: function(customerObject) {
            var cname = customerObject.get("customername");
            var cnumber = customerObject.get("customernumber");
            ...
        }
    });
};
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! To make sure I understand, I thought this code would query parse to look to see if the number entered in the input box (#searchnumber) matched the number stored against (customernumber) in parse, if a match was found then I could then do something further with the matched object.
Yes, the equalTo will match customernumber with the value from #searchnumber, and only return an object if match is found.
Ok, thats what I'm looking to achieve, when i run the code though and enter "21" intot he input box, no object seems to be returned? kudosoo.com/findfriends.html
So, show me a screenshot of your data browser for a row where customernumber is 21
What if you comment out the equalTo line and use retrieve.find instead? "customerObject" should in this case be an array containing 1 object. Can you check that it does?
|

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.