Unable to save variable in localStorage in Ionic 2

In my ionic2 application, I have successfully integrated Google Maps functionality. However, I am currently struggling with saving the formatted address to local storage.

Whenever I reload the page, I consistently encounter the following error:

Uncaught TypeError: Cannot read property 'set' of undefined

Below is a snippet of my JavaScript code:

ionViewDidLoad(){
    let loader = this.LoadingController.create({
      content: 'Getting your Location'
      });
    loader.present().then(()=>{
    //Rest of the JavaScript code goes here...

I am perplexed by the line

this.storage.set('user_lng', this.lng);
which saves the user's latitude and longitude to the local storage without any issues.

Answer №1

Ensure that the IonicStorageModule is correctly imported within your import section.

import { IonicStorageModule } from '@ionic/storage';


@NgModule({
  declarations: [

  ],
  imports: [

    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [

  ],
  providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})

Answer №2

this obtained from this.storage where you are invoking it is not within the correct scope, thus lacking the property storage. A solution would be to store the storage value in a global variable before calling the function:

    import { IonicStorageModule } from '@ionic/storage';
    var storage = this.storage;

    ionViewDidLoad(){
        ....
        //replace occurrences of this.storage with the 'storage' variable
        ....
    }

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

"Learn the steps to include a success message in an HTTP POST response once a button has been

Currently, I am utilizing nodemailer along with express... I aim to incorporate a text message and loading indicator on the front-end to indicate whether the form submission is in progress or not. .ts deliverEmail(value) { console.log('em ...

Choosing an element in JQuery based on the value of its style property

I have three divs with the same class but different styles. I need to select only the third one using JQuery. <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-div ...

"Sending the real object as a parameter to a function when triggering an event

Whenever I click a button, I want to pass the 'contract' object into it. <td class="col-md-1" colspan="1" style="text-align: center; vertical-align: middle;"> <button class="btn btn-primary" data-ng-click="removeContract(ctrl.select ...

Turn only one bracket on the accordion

When clicking on a specific header, I want only one chevron to rotate instead of all the chevrons rotating. I am currently unsure how to specify which chevron should rotate individually. My project is in ASP.NET MVC 5 and I am using razor view to loop th ...

What is the best way to send two arguments to a Python function that is being invoked by an Ajax function?

My current project involves creating a web app using Flask. I am facing an issue with calling a specific function when a button is clicked without the page reloading: @app.route('/add_song_to_playlist') def add_song_to_playlist(pl_id, s_id): ...

Unable to retrieve a list of objects from the model map when accessing the FTL file

Recently started using FTL (FreeMarker template) and encountering an issue while sending a List of objects from my controller via model map. Below is the snippet of my controller code: Controller Code @RequestMapping(value="/xxx") public String myMethod( ...

Issue with React.js button functionality not functioning as expected

import React, { Component } from 'react'; import './App.css'; class App extends Component { constructor(props) { super(props); this.state = { items: [] } } addItem(e) { var itemArray = this.state.items; ...

What is the best way to deduct pixels from numbers using JavaScript?

Currently, I am attempting to adjust the height of the footer based on the height of another div element. My approach involves utilizing the .css("height") function. However, I am encountering difficulty as the function does not seem to return the value i ...

Is the memory usage of node.js proportional to the number of concurrent requests, or is there a potential memory leak?

Running the following node.js code: var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type': 'text/plain'}); res.write("Hello"); res.end(); }).listen(8888); Upon starting the server ...

Node package and node-sass encountered an issue

Having trouble with this error showing up in the javascript console. I've already tried removing node_modules, reinstalling it, and running npm rebuild node-sass without success. Any guidance on what steps to take next would be greatly appreciated. a ...

The alignment of data-table columns appears to be off to the right

Currently, I am working on data-tables and have successfully created one. However, I am facing an issue with aligning the data to the right of some columns. Snippet: var data=[ { "amount": 518212, "billdate": "2018-08-04", "outlet": "JAYA ...

Issues with running NPM script for compiling TypeScript code

[UPDATE] Initially, I resolved this issue by uninstalling tsc using npm uninstall tsc (as recommended in the response marked as the answer). However, the problem resurfaced after some time, and eventually, I found success by utilizing Windows Server for L ...

Adjusting the focal point of a brochure on open street map

I am currently working on initializing a map in an Angular application using leaflet with openstreetmaps. I have set the center of the map so that it is displayed to the user when they open the site. However, I am now trying to figure out how to dynamica ...

Using selected option from dropdown as a value for a hyperlink

I'm attempting to transfer the chosen value from a dropdown menu to my link. This way, when I click on the specific link, it should trigger the corresponding action based on the ID selected from the dropdown. The alert shows the value, but now I need ...

Converting a JS string into HTML markup

I recently developed a basic web application to manage telnet connections with routers using Node.js, Express, and WebSockets. After establishing a connection, the terminal stream is transmitted through a websocket as UTF-8 strings. However, my current is ...

Tips for preserving the state of the Material-UI AutoComplete during component re-renders?

Currently, I am utilizing the Material-UI v4 AutoComplete component along with the renderOption prop in order to display a checkbox item option. The issue arises when the onChange event occurs and updates a hook in the parent component causing a re-rende ...

Why is my PHP code returning empty in Chrome but showing values in Firefox? What steps can I take to troubleshoot this

My initial assumption was that it should be server-side specific and not introduce differing behaviors dependent on the browser. The code snippet below results in null, but when tested in Firefox, it contains data that is later processed into a table. $H ...

Learn the steps to assign a Base64 URL to an image source

I am currently facing an issue with an image that is being used with angular-cli: <img src="" style="width: 120px; padding-top: 10px" alt="" id="dishPhoto"> The image has a Base64 url named imgUrl. My intention is to set the image source using the ...

Parent window login portal

I have just started learning how to program web applications, so I am not familiar with all the technical terms yet. I want to create a login window that behaves like this: When a user clicks on the Login button, a window should pop up on the same page t ...

Extracting a precise data point stored in Mongo database

I have been struggling to extract a specific value from my MongoDB database in node.js. I have tried using both find() and findOne(), but I keep receiving an object-like output in the console. Here is the code snippet: const mongoose = require('mongoo ...