Analyzing and adding Angular JSON information

I'm struggling to understand why the compare function and insert function in my code aren't functioning correctly. I suspect that one reason for this could be that the function is not being called by the button. How can I confirm this? It seems like alerts are not working in my Angular code.

This is the content of my loginHandler.js file:

(function() {
var app = angular.module('loginHandler', []);

app.controller('LoginController', ['$http' ,function($http){
  var database = this;
  this.validated = false;
  database.dbData = [];

      $http.get('/resources/DatabaseObject.json').success(function(data){
          database.dbData = data;
      });

  this.login = function(credentials){
      loggedInUser = credentals.username;
      validated = true;

      $http.get('/resources/DatabaseObject.json').success(function(data){
          $scope.dbData.push({"123":{"username":"meph","ip":"123","timeLastLogin":"123"}}, "session");
      });
  };

  this.checkSession = function(){
      var ip = "123456";

      if(session.ip.equals(123456)){
          return true;
      }
  };
 }]);
})();

Both the login and checkSession functions are not working as expected.

This is the content of the DatabaseObject.json file:

[{
"users": {
    "cra": {
        "firstName": "Jn",
        "lastName": "it",
        "username": "dmph",
        "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="374556590106774e565f58581954585a">[email protected]</a>",
        "ip": "192.168.1.1",
        "password": "1234",
        "computerName": "os",
        "prviateKey": "1D3RW12390ASLEWRQ1235"
    },
    "mus": {
        "firstName": "James",
        "lastName": "mh",
        "username": "ch",
        "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5df8384f5d2d8d4dcd99bd6dad8">[email protected]</a>",
        "ip": "192.168.1.1",
        "password": "11212",
        "computerName": "ops",
        "prviateKey": "1D3RW12390ASLEWRQ1235"
    }
},
"session": {
    "123456": {
        "username": "crh",
        "ip": "123456",
        "timeLastLogin": "123456787654"
    }
}
}]

Here is the relevant HTML code:

   <html lang="en" ng-app="gemStore">

<!-- Add your unique and relevant HTML content here -->

</body>
</html>

I am unsure about the functionality of the submit button. Could someone please review my code and offer assistance? I have been attempting various solutions without success.

http://plnkr.co/edit/zYSFmQSU83FMC9UcXhM4?p=preview

Answer №1

The issue with the push function arises when you attempt to push data into the non-existent $scope.dbData, while the actual data is in database.dbData.

Another problem is encountered when you try to compare against session.ip, although this variable has not been defined in the code.

Answer №2

The reason for the issue is likely due to your usage of this. instead of $scope.

Additionally, you need to remember to pass $scope into your parameters...

app.controller('LoginController', ['$http' ,function($http){

Instead, it should be:

app.controller('LoginController', ['$http, $scope' ,function($http, $scope){

Furthermore, make sure to use: (not this)

$scope.login = function () { /* ... */ };
$scope.checkSession = function () { /* ... */ };

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS - Monitoring the Completion of Rendered Trees in Recursive ng-includes

My current project involves rendering a hierarchical tree recursively using ng-include. If you're interested, you can take a look at this fiddle. The issue I'm facing is that the link function within my code gets called before the rendering proc ...

I can't seem to figure out why I constantly struggle with adding a check mark to a checkbox using

Take a look at the code I've provided below : HTML : <input type="checkbox" name="xyz[1][]" id="sel_44" style="margin:2px;" value="12345" onclick="myClick(this)"> Javascript : <script> $('#sel_44').attr("checked", true); < ...

Encountering a SyntaxError with the message 'Unexpected token' while trying to require a module in strict mode from JSON at position 0

Within the index.js file, the following code is present: 'use strict'; const config = require('./config'); In the config.js file, the code looks like this: 'use strict'; const config = new function() { this.port = 3000; ...

Implementing Bootstrap in Angular 2 using vanilla JavaScript/ES6: A step-by-step guide

Currently, I am in the process of developing an Angular 2 application without TypeScript and facing difficulties with bootstrapping it as I cannot find any relevant examples. My app, which does not include the bootstrap() function, starts off like this: ...

The NestJs project fails to display the updates when using the "tsc" command before running either "npm run start" or "npm run start:dev"

As a beginner in nestjs, I decided to start a tutorial to learn more about it. However, whenever I make updates or changes to my code, I don't see any changes reflected in the results. Can someone please assist me with this issue? Below are my tsconfi ...

Understanding the variations in behavior of the history.go(-1) method across various browsers

Is history.go(-1); consistent across all browsers? I've noticed different behaviors on various browsers. In my code, I have a line similar to javascript:history.go(-1); On the first page, there are three checkboxes. Users can only select two of them ...

bootstrap thumbnail displayed without a border

I have decided to incorporate Bootstrap into my latest project: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS ...

Using React and Redux to update the state of an object with the current state

Upon mounting my component, I make a call to an API and upon success, the state is updated with the following data: { "brief":"No brief given", "tasks":[ { "_id":"5c74ffc257a059094cf8f3c2", " ...

Apply SetTimeout exclusively for desktop devices

A website I'm working on has a background video from YouTube using YTPlayer. To enhance the user experience, I have implemented a CSS spinner that displays while the page is loading. However, I noticed that the spinner disappears before the video fini ...

Error 400 encountered when attempting to log in with React through Google on mobile devices

Currently, I am implementing the React Google Login package to handle user authentication on my website. Surprisingly, it functions perfectly on desktops; however, when tested on mobile devices, an annoying Error 400: redirect_uri_mismatch pops up. Despi ...

Ways to emphasize the contrast between two texts using CSS

One method to highlight specific parts of text using CSS can be found in this script: span.highlight { background-color: #B4D5FF; } However, what if we want to highlight the differences between two strings? Take for example these two strings: this ...

Ways to access UserProfile in a different Dialogio

For the implementation of a chatbot, I am utilizing Microsoft's Bot Builder framework. However, upon implementing an alternative path to the dialog flow, I noticed that the user's Profile references are getting lost. Here is the code snippet fr ...

Using urllib.urlopen in place of javascript

I'm curious if there is a Javascript module that functions similarly to Python's urllib. Specifically, I am looking for something along the lines of: urllib.urlopen(url, data) This function would return an object that allows for a blocking fetc ...

Utilizing the datepicker options function within a different function

I'm working on a function that utilizes a promise to retrieve data from an asynchronous ajax call: $("#mySelect").on('change', function() { var mySelectValue = $('#mySelect').val(); var promise = getAvailableDates(mySe ...

Looking for assistance with parsing a JSON string within a SQL stored procedure and extracting the values of its attributes

Is there a more efficient way to parse JSON strings in a SQL stored procedure and update corresponding fields of tables based on the attributes? For example, I want to extract specific information from a JSON string that looks like this: {"category1":{"f ...

NodeJs is facing a challenge with the global variable for retrieving all routes methods after successful sign in

I have created a blog page with options for signing up and signing in. I used the req.locals.<name> method Using the GET method. Code snippet from server.js app.get('*',(req,res, next) => { res.locals.user = req.user || null; ...

Instructions on inserting a new row beneath a selected row using jQuery

https://i.stack.imgur.com/AlwHm.png When the second column is clicked, the method below is called: function getListOfList(primaryId, isFlat, col) { alert(primaryId) $.ajax({ url : ("/" + serverURL + "/dataGrid/getChilds"), ...

Looking into the field of a document that is referenced

Within the database, there are two collections: 'actors' and 'movies' Here is an example of one actor: { _id: ObjectId("54f38bd9b814dca762778032"), name: { first: 'Jason', last: 'Statham' } } And her ...

In case the desired property is not detected in the object, opt for an alternative property

Here is an example object: Object -> Content: <h1>some html</h1> Url : A url In the code below, I am checking if a specific URL exists in the given object: var checkURLInArray = function(url, array) { for(var i in array) { ...

Running the command "nexrjs create next-app" successfully generates all the necessary directories for the

After trying to install NextJS using both methods npm install next react react-dom and npx create-next-app appname, I noticed that my project directories are different from what they are supposed to look like: Instead of having pages, api(_app.js, index.j ...