What could be the reason for the lack of read or write functionality in $cookies for angular version 1.6.6?

I am relatively new to working with Angular. I have started a test project and my goal is to store user authentication in cookies. Despite my best efforts, I keep encountering an undefined error when attempting to retrieve the stored value. I am not sure what exactly is wrong as I have followed the steps diligently and searched through various responses on stackoverflow. Thank you for any insights you may provide.

I have also enabled Chrome cookies to read and write data by following this link: https://i.stack.imgur.com/Bh3fr.png

Currently using Angular version 1.6.6

angular.module('LoginCtrl', ["ngCookies"])
    .controller('LoginController', function($rootScope, $location, $scope, 
    $http, $cookies)
{

$scope.loginformData = {};
$scope.loginmessage='';
$scope.errorlogin=false;
$scope.loginUser = function()
{
Login.login($scope.loginformData)
.then(function(data)
{
$scope.loginmessage = data;
if(data!=='Wrong Email Or Password!')
{
$cookies.put('myFavorite', 'oatmeal');
var favoriteCookie = $cookies.get('myFavorite');
console.log(favoriteCookie); // returns undefined
}
else
{
$scope.errorlogin = true;
console.log('Error');
}
});
        };


    });
<script src="https://code.angularjs.org/1.6.6/angular.js"></script>
<script src="https://code.angularjs.org/1.6.6/angular-cookies.js"></script>
<base href="/index.html"/>
<div ng-app="LoginCtrl">
  <div ng-controller="LoginController">

  </div>
</div>

Answer №1

Make sure to utilize

$cookies.putObject('myFavoritec', 'oatmeal');
when using angular versions higher than 1.4.

This also applies to retrieving the data.

$cookies.getObject('myFavoritec');

Answer №2

Upon conducting a thorough examination of angular-cookies.js, I meticulously debugged line by line. It became evident that the cookiePath was not being assigned the correct value.

  var cookiePath = $browser.baseHref();
  console.log(cookiePath); // returns /index.html 

This incorrect path caused issues with storing cookies, as the path should have been the root of my project /

The erroneous path originated from my index file, where I had mistakenly set the base tag to "/index.html"

<base href="/index.html">

After updating the base tag to point to the root, the issue was resolved.

<base href="/">

And now, it functions as intended.

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

Utilize a while loop in JavaScript to trigger a message when a variable dips below zero

Forgive me if I seem clueless. I am a beginner in the world of Javascript and am currently experimenting with loops. At the moment, I am toying around with this particular piece of code: <!DOCTYPE html> <html> <body> <button oncl ...

Is it possible to restrict the acceptance of certain variables from a CSV file in Django form fields, instead of accepting

In my Django model, I have two fields for latitude and longitude which are both declared as CharField. The model needs to accept multiple coordinates, so in the UI form, I enter these coordinates separated by commas. I then split this char input in a funct ...

The functionality for selecting text in multiple dropdown menus using the .on method is currently limited to the first dropdown

Having trouble selecting an li element from a Bootstrap dropdown and displaying it in the dropdown box? I'm facing an issue where only the text from the first dropdown changes and the event for the second dropdown doesn't work. <div class="dr ...

Having trouble with data retrieval from MySQL using PHP and Angular on certain mobile devices, although it functions properly on desktops and laptops

The data retrieved from the mysql database is functioning properly on desktop and laptop, but not on mobile devices. The following HTML code is present in the html file: <table class="table table-default"> <thead> <tr> & ...

Tips on extracting JSON information in JavaScript when the key name is unspecified

I have a challenge in parsing JSON data where the property name is unknown. Below is a snippet of my code, and I need your help in figuring out how to retrieve all data with an unknown property. datas.data.videoGroup.past, then utilize a loop $.each(data ...

Delete outdated information using Google Apps Scripts when the date is less than the current date plus a specified number of days

I have a Google Sheet where I need to filter out entries based on the number of days since the last check. Specifically, I want to keep only those entries where the number of days since the last check is greater than 10. You can find the Sheet here. fu ...

Switching hamburger menu icons in Vue.js

I am trying to change a hamburger menu icon in my header to a cross icon when it is clicked. I have created a function in the computed property of my Vue template, but I'm not entirely sure about the implementation. How can I achieve this? Below is t ...

Here is a way to attach a function to dynamically generated HTML that is returned in an AJAX request

As I was working on a new function development project, I encountered a situation where I had to add a function to dynamically generated HTML through an ajax call. The following is a snippet of the code: $.ajax( 'success': function(data){ ...

Error: React Select input control is throwing a TypeError related to event.target

Having trouble changing the state on change using a React Select node module package. It works with regular text input, but I can't quite get it to work with this specific component. The error message "TypeError: event.target is undefined" keeps poppi ...

issue with Knex query output when integrated with Express.js

Below is the Knex raw SQL query that I have written. knex.raw("select group_concat(details_odishagovtjobdetails.more_info) as more_info,\ scrap_odishagovtjobs.start_date,scrap_odishagovtjobs.last_date,scrap_odishagovtjobs.post_name,\ ...

Showing error messages in Angular when a form is submitted and found to be invalid

My form currently displays an error message under each field if left empty or invalid. However, I want to customize the behavior of the submit button when the form is invalid. <form #projectForm="ngForm" (ngSubmit)="onSubmit()"> ...

Ways to showcase various links in Angular based on API response

I am currently learning Angular and exploring how to convert an ASP.NET application into Angular. My goal is to dynamically display different links to users based on their group membership. I have a Web API (ASP.NET Web API) that can provide user informati ...

Facing continuous 404 errors while working with nodejs and express

While attempting to collect data from a local host webpage's registration form that captures user information, I encounter an issue upon pressing the submit button A 404 Error is displayed stating "page not found", preventing the collection and out ...

What is the best way to include and control persistent URL parameters when making Ajax requests?

Imagine having two different resource lists on your website: one for users and the other for groups. As you navigate through each list in increments of 10, it's important to preserve the state of the list so that when you share the URL with someone el ...

Cross-platform mobile browsers typically have scrollbars in their scrollable divs

I have successfully implemented scrollable div's on a page designed for tablets running Android 3.2+, BlackBerry Playbook, and iOS5+ (except iPad 1). However, I would like to add scrollbars to improve the user experience. For iOS5, I can use the suppo ...

Unraveling Angular: Generating a Random Sequence of Symbols from an Array Multiple Times

A collection of symbols from A to E is at my disposal. My task is to display each symbol using ng-repeat, but the order in which they appear should be random and each symbol needs to be repeated n times (let's say 4). ...

% unable to display on tooltip pie chart in highcharts angular js

https://i.stack.imgur.com/Ccd7h.png The % symbol isn't displaying correctly in the highcharts ageData = { chartConfig: { options: { chart: { type: 'pie', width: 275, height: 220, marginTop: 70 ...

Easily transfer files without the need to refresh the page by utilizing the power of AJAX

In my page file-upload.jsp, I have the following code snippet: <form action="" id="frmupload" name="frmupload" method="post" enctype="multipart/form-data"> <input type="file" id="upload_file" name="upload_file" multiple="" /> <in ...

Swap out the <a> tag for an <input type="button"> element that includes a "download" property

I have been working on a simple canvas-to-image exporter. You can find it here. Currently, it only works with the following code: <a id="download" download="CanvasDemo.png">Download as image</a> However, I would like to use something like th ...

Parse the JSON data response as needed in ReactJS

var mydata = [ { source: 11, Registernumber: ">RT-113, <RT-333", jul1: 1004 }, { source: 11, Registernumber: ">RT-113, <RT-333", jul2: 1234 }, // Rest of the data entries... ]; Can we transform the above JSON ...