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);
});
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 thejqXHR
; 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: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:Or something along those lines. From what I'm seeing, the
Where
would be called to filter rows (as expected), and thethis
context would be the current object to be filtered.