SQLike query on JSON returning 3996 objects

191 Views Asked by At

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);
});
3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On
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;
                     ^^^
0
On

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

return this['username'] === username;