0

I am trying to query a JSON object in order to determine a username. However, when I run the SQLike i have 3996 objects returned, none of which matching the correct parameters that I have asked to be returned. Would anyone be able to point out where I have gone wrong and as to why I am receiving 3996 returned objects and further, how can I strip it out to only return a username?

user.json

[
    {
        "id":1,
        "fName": "Sam",
        "lName": "Street",
        "username": "sam",
        "password": "123"
    },
    {
        "id":2,
        "fName": "Matt",
        "lName": "Mantle",
        "username": "matt",
        "password": "123"
    },
    {
        "id":3,
        "fName": "Dev",
        "lName": "Mode",
        "username": "dev",
        "password": "123"
    }
]

The javascript that I call on a submit of a form

$("#login_form").submit(function (e) {
    // prevent default action
    e.preventDefault();
    var form = $(this);
    var username = form.find("#username").val();
    var password = form.find("#password").val();

    // need to pass this as generic function when finished
    var userData = $.ajax({
        async: false,
        url: 'user.json',
        type: "GET",
        dataType: "json",
        success: function (data) {
            console.log(data);
        }
    });

    var sel = SQLike.q(
        {
            Select: ['*'],
            From: userData,
            Where: function () { return this.userData = username }
        }
    )
    console.log(sel);
});
Sam Street
  • 306
  • 2
  • 14
  • 2
    It appears that you'll be going to check the password client-side. Don't do that. – Bouke Feb 19 '14 at 19:54
  • 2
    Hm, just to be sure: `return this.userData = username` <-- are you sure this is `=` and not `==`? – fge Feb 19 '14 at 19:54
  • 1
    @bouke - I am not doing any log in or anything along those lines, this is purely for learning purposes as I just wanted to learn how to query a JSON file. @fge - I have used `return this.userData = username` and not `return this.userData == username` – Sam Street Feb 19 '14 at 19:59
  • 1
    Well, do use `==`... Here you affect `username` to `this.userdata`; the result of the function will always be true (unless `username` is empty or null) – fge Feb 19 '14 at 20:01

3 Answers3

2
return this.userData = username
                    ^^^

= is assignment and since you are returning an assignment it will be true and every record will be returned.

return this.userData === username;
                     ^^^
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

Apart from the error pointed out by epascarello, you'd also want to check the value of 'username', not 'userData':

return this['username'] === username;
Bouke
  • 11,768
  • 7
  • 68
  • 102
1

Consider this a "partial" answer because I am not familiar with that library; I'm merely going off of their documentation.

Ajax is normally asynchronous, and I wouldn't recommend making it synchronous.

The $.ajax returns the jqXHR; it is not the JSON data you're expecting to get.

To make life easier, I would move your SQLike code in to the success function of the ajax call, like so:

// need to pass this as generic function when finished
var userData = $.ajax({
    url: 'user.json',
    type: "GET",
    dataType: "json",
    success: function (data) {
        console.log(data);
        var sel = SQLike.q(
        {
            Select: ['*'],
            From: data,
            Where: function () { return this.data === username }
        }
    }
});

Note the non-assignment equals === already explained by epascarello.

Now for the "partial" answer portion: while I'm not familiar with this library, looking through their documentation makes me wonder if your Where function should look like this:

return this.username === username

Or something along those lines. From what I'm seeing, the Where would be called to filter rows (as expected), and the this context would be the current object to be filtered.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85