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

When attempting to reload a single page application that utilizes AJAX, a 404 error is encountered

Recently, I've been working on coding my personal website and have successfully created a single page application using ajax. The content is dynamically loaded between the header and footer whenever a navigation bar link is clicked. To enable the back ...

Receive information on browser activity

Is there a way to trigger an event when the following actions occur: Pressing the reload icon in the browser Pressing the Back or Forward icon in the browser Selecting the Reload menu item from the browser context menu Selecting the Reload option from t ...

Storing the outcome of a connection in a variable using Node.js

I am facing an issue with saving a function return in a const so that I can utilize the information outside of the function scope. Below is a snippet of code to better explain my problem: const express = require('express') const app = express() ...

How do you obtain the string name of an unknown object type?

In my backend controllers, I have a common provider that I use extensively. It's structured like this: @Injectable() export class CommonMasterdataProvider<T> { private readonly route:string = '/api/'; constructor(private http ...

Applying multiple select filters in AngularJS to refine a scope

Check out my fiddle example here: http://jsfiddle.net/mwrLc/12/ <div ng-controller="MyCtrl"> <select ng-model="searchCountries" ng-options="cc.country for cc in countriesList | orderBy:'country'"> <option value="">Country ...

Extension for capturing videos on Chrome or Firefox

I am interested in developing a Chrome or Firefox extension that can capture video from a window or tab. My goal is to record full screen videos, such as those on YouTube, for offline viewing similar to a DVR for online content. Creating an extension see ...

Is it a good idea to utilize triggers when bootstrapping an application?

Currently, I am in the process of developing an internal web application for managing building parts. The main focus is on a table containing projects that are interconnected with other tables. Whenever a new project is created by a user, my goal is to ini ...

Display items inside containers using angularjs ng repeat and bootstrap styles, however, the layout is not being rendered correctly

I am struggling to display products in boxes on my ecommerce website, similar to how they appear on other platforms. Although I am utilizing AngularJS ng-repeat and bootstrap classes, the layout of the products is not coming out as intended. Take a look ...

Having difficulty sending emails with attachments using AngularJS

While using Angular, I encountered an issue when sending an email with an attachment. The response I received contained the data code of the file instead of its actual format. See example: https://i.stack.imgur.com/vk7X8.png I am unsure what is causing t ...

How do I redirect with a GET method after calling the *delete* method in Node / Express server?

As someone new to AJAX and Node, I encountered a dilemma that I hope to get some guidance on. Here's the issue: I have a DELETE ajax call that removes a row from the database and I want to redirect back to the same route with a GET method afterwards. ...

Adjust the color of the active link on the page using Angular and CSS

I have a project that I need to modify by adding a sub menu that appears on every page but is only coded once. My goal is to highlight the link for the current page, all within one HTML snippet. Although the list renders correctly, I'm struggling to g ...

Checkbox selection can alternate based on conditions for formgroup controls

My FormGroup named 'fruits' has been set up fruits: FormGroup = formBuilder.group({ numberOfFruits: [0,Validators.min(0)], apple: false, mangoes: false }); Here is the corresponding HTML code: <div formGroupName ...

Ionic 2: Unveiling the Flipclock Component

Can anyone provide guidance on integrating the Flipclock 24-hours feature into my Ionic 2 application? I'm unsure about the compatibility of the JavaScript library with Ionic 2 in typescript. I have searched for information on using Flipclock in Ionic ...

Once the div content is reloaded using AJAX, the refreshed HTML suddenly vanishes

My JS code reloads the div every 2 seconds: var auto_refresh = setInterval(function() { $('#indexRefresh').load('/includes/index_refresh_include.php?_=' + Math.random()); }, 2000); After that, I have an AJAX request that loads mor ...

Combining a form and table on a single webpage, I am curious how to utilize the form to update the table effectively using Express and Node.js

Seeking guidance in website development as I face a particular challenge. I aim to create a dynamic search page for a website. The form should allow users to select a location or category, with the same page updating with relevant results. Despite succes ...

Extending a Sub-Object in JavaScript

I'm attempting to include new attributes to the sub-object within object1. However, I am facing issues with it being overwritten. const object1 = { a: 1, b: 2, c: 3, sub: { e: 1, f: 2 } }; const object2 = Object.assign({ j: 4, ...

Directive for integrating Amcharts with Angular JS

I have created a custom directive specifically for displaying a single chart using Amcharts. angular.module("App").directive('myElem', function () { return { restrict: 'E', replace:true, temp ...

When package.json is imported, files are not compressed

Currently, I am working on developing a cli package and when it comes to displaying the version, I am utilizing the version imported from package.json. Upon running tsc, the structure of the dist folder appears as follows: /dist --/package.json --/README. ...

jQuery deduct a value from a given value

i have a full duration that is divided into multiple sections, the user needs to input the duration for each section i would like the system to calculate and display the remaining duration from the full duration, regardless of whether the user fills out ...

Enhancing game menus for various mobile screen sizes

I've noticed a consistent pattern in the games I download from the Google Play Store - they all have a background image at the menu with clickable buttons. When testing these games on different devices, I found that the backgrounds didn't stretch ...