The data displayed in the Developer Command Prompt differs from the values shown in the Browser Console

I am currently working with an Observable that checks if a key is stored in the browser's local storage. The value from this observable is utilized in another observable. However, I have observed that there are discrepancies between the values displayed in the logs of the Development Command Prompt and the browser console.

checkIfUserIsStoredInLocal(): Observable<boolean> {
  const localStorage = this.document.defaultView?.localStorage;

  if (localStorage!== undefined) {
    if (localStorage.getItem("currentAppUser") !== null) {
      var currentUserLocal = localStorage.getItem("currentAppUser");
      this.currentUser = JSON.parse(currentUserLocal!);
      this.currentUserProfileBS.next(this.currentUserProfile);
      console.log("User exists in local");
      return of(true);
    };

    return of(false);
  }

  return of(false);
}
initializeComponentLogin(): Observable<boolean> {
  var initialization: Observable<any> = this.checkIfUserIsStoredInLocal().pipe(
    take(1),
    tap(x => console.log("data from $ is " + x)),
    tap(() => console.log("Current user is " + this.currentUser)),
  );

  return initialization;
}

Output from Developer Command Prompt: https://i.sstatic.net/jpPNBOFd.png

Output from Browser Console:

https://i.sstatic.net/53PnuwYH.png

I came across this issue while attempting to use the data stored in local storage within another observable using concatMap, but encountered an undefined error. I am uncertain if the observables are being chained correctly. My goal is to first check if the user exists in local storage, then authenticate that user to the server, followed by retrieving additional details about the authenticated user from the server.

Answer №1

If you are utilizing SSR, be aware that the majority of your code will execute on the server side. In NodeJS (which runs on the server side), there is no access to localStorage, as it is a feature exclusive to the client-side browser environment. When incorporating browser APIs like localStorage into Angular SSR, it is crucial to encapsulate this code within either afterRender() or afterNextRender() to ensure it is not mistakenly executed on the server side.

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

Error: Unable to inject UrlHandlingStrategy as no provider was found

I recently upgraded my application to Angular 14 and encountered a challenging error. Despite configuring RouterModule for Root and child with lazy loading, I am now facing a circular dependency issue related to the Router. I'm unsure how to further d ...

Oops! You can't switch to `multiple` mode on a select dropdown once it has already

Here's where the issue lies: This is the code I am referring to: <div fxFlex.gt-lg="100" fxFlex="100" *ngIf="requestAction == 'add'"> <div class="pb-1"> <md2-select plac ...

There are two distinct build commands in Angular that can produce unique JS file hashes

I'm currently working on a new Angular project where I have two different npm commands specified in my package.json. Both of these commands run ng build --output-path=..., but with different flags to generate build files in separate locations. Is the ...

Problem with deploying Angular 2 project on Github

My process began with executing this command: npm i -g angular-cli-ghpages Then, I proceeded to run the following command: ng github-pages:deploy Despite my efforts, I consistently encounter the same message: The specified command github-pages:deploy is ...

What is the best way to send data to a child component in Angular, including components that can be

There are two components in my project: the parent component and the child component, each with their own unique markup. It is crucial for this example that the child component fetches its own data to render and not be provided by the parent. Currently, th ...

Launching an Angular application beyond the typical Angular framework

I want to develop an Angular 7 app by following a tutorial that provides the default page template. ng new my-app After reviewing it using ng serve --o and confirming its functionality, I am struggling with how to deploy it outside of Angular. I tried ru ...

What is the best way to utilize the useMemo function within this scenario?

I am facing a challenge while trying to change a const variable to const using useMemo. I have attempted examples from the documentation and various tutorials, but none seem to work for me. This should be straightforward... const bottomSheetOptions: Bottom ...

Tips for restricting additional input when maximum length is reached in an Angular app

In my Angular 6 application, I am working on implementing a directive that prevents users from typing additional characters in an input field. However, I want to allow certain non-data input keys such as tab, delete, and backspace. Currently, I have an if ...

Can you outline the key distinctions between ref and reactive in Vue.js?

After diving into Vue.js recently, I'm finding myself struggling to completely understand the key distinctions between using ref and reactive. Can someone clarify when it's appropriate to utilize ref versus reactive? ...

Testing Angular2 / TypeScript HTTPService without Mocking: A Guide

import {Injectable} from '@angular/core'; import {Http} from '@angular/http'; @Injectable() export class HttpService { result: any; constructor(private http:Http) { } public postRequest(){ return this.http.get('h ...

Developing a separate NPM package for a portion of the app causes an increase in the total app size

Our projects utilize create-react-app and Typescript, resulting in the emergence of a collection of commonly used React components. To enhance maintenance and reusability, we decided to extract these components into their own NPM package named PackageA. Su ...

No input in ng2-select2

How can I reset the value in ng2-select2 by clicking on a button? Here is my current HTML code: <select2 id="bill" [data]="colBill" [options]="option" (valueChanged)="onBillArray($event);"> The corresponding Typescript code is: this.arrBill = []; ...

Exploring i18nNext integration with antd table in React

Presently, this is the UI https://i.stack.imgur.com/CMvle.png App.tsx import "./styles.css"; import MyTable from "./MyTable"; export default function App() { const data = [ { key: "1", date: "2022-01- ...

Using Typescript to define unions with multiple callback types

While in the process of converting my code to TypeScript, I encountered a dilemma. I created a generic foreach function that can handle arrays and objects, with different types of callbacks for iteration. However, I'm unsure how to specify the type wh ...

Issue with Angular app's connectivity to Node backend app within kubernetes cluster

I currently have a backend Node application operating within a Kubernetes cluster (specifically running locally with minikube). The backend service is set up as shown below: apiVersion: v1 kind: Service metadata: name: flow-backend-service spec: select ...

Issue: The method onBeforeCompile is not found in the LineDashedMaterialParameters type

I am interested in developing a rotating cube with concealed dashed lines as shown below. https://i.sstatic.net/0U2im.gif However, I encountered the following error message in VS Code (parameter) shader: any Argument of type '{ color: ColorReprese ...

Having trouble with importing ResizeSensor library into typescript

In my TypeScript app using webpack, I am attempting to utilize css-element-queries/ResizeSensor. After adding the npm package, which includes a .d.ts file, I encountered an issue when writing the following code: new ResizeSensor(element, cb); Instead of ...

What is the most effective approach for handling user input delays with Async promises?

New to Vue.js and currently exploring the following challenge: I have a component named PopUp which can be displayed or hidden using functions of the same name. My goal is for the popUp.show() function to return a promise that contains information about ...

What causes a compile-time error in visual code when using generics with multiple types?

I'm attempting to develop a function with a generic type that takes one parameter with multiple types. It seems straightforward, but when I combine certain types, I encounter compile-time issues in Visual Studio Code. Check out my example below: Th ...

The TypeScript compiler throws an error when encountering nulls in conjunction with the isNull function

Whenever I set strictNullChecks: true in tsconfig.json and utilize the isNull function for null checks, the compiler throws the error TS2531: Object is possibly 'null'. Interestingly, isNull doesn't trigger any errors in VsCode, however, the ...