Using Typescript to set the image source from a pipe

I've been working on creating a custom pipe similar to the code below:

@Pipe({
   name: 'imagePipe'
})

@Injectable()
export class ImagePipe {
  constructor(public someService: SomeService, public storage: Storage) {
} 

  transform(value: any, arg: any) {
     if ((value != null) && (value!=arg)){
            return this.storage.get(value).then((val) => {
                            console.log('Your source is', val);

                })
    }
  }
}

The main purpose of this pipe is to look for a specific value in storage and then set the URL for an image. I'm using it like this:

<img src="{{info.title | imagePipe : otherTitle | async}}" width="45" height="120"/>

Although the console displays the correct value, unfortunately, the image URL remains null.

Answer №1

My assessment indicates two errors in this code snippet:

 if ((value != null) && (value != arg)){
    return this.storage.get(value).then((val) => {
        console.log('Your source is', val);
    });
}

Firstly, if the condition inside the if statement evaluates to false, nothing is returned. Secondly, I recommend loading storage.get into a "class var" and calling it from there for better organization.

Answer №2

The answer lies within

@Pipe({
    name: 'imageConverter'
})

@Injectable()
export class ImageConverter {
    constructor(public service: SomeService, public database: Storage) {}

    transformation(value: any, argument: any) {
        if ((value != null) && (value != argument)){
            return this.database.retrieve(value).then((result) => {
                console.log('The source you seek is', result);
                return result;
            })
        }
    }
}

Certain elements of the response were inaccurate

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

Generate random entries from an object based on specific criteria and append them to a fresh object using Typescript

Experimenting with Ionic2 and Typescript has been my recent focus. I have an object that contains various meals, calorie counts, and meal types (such as vegan). This is how the object looks: [ { "id":14093, "name":"Proteinshake mit Wasser ...

Troubleshooting problems with Angular 6 update through the command "ng update"

https://i.stack.imgur.com/LuPSs.png I am currently in the process of upgrading from Angular version 5 to 6. When using the "ng update" command, I encountered a problem that is shown in the attached screenshot. Can someone guide me on how to resolve this ...

Exploring TypeScript: Implementing a runtime data mapping in place of an interface

Take a look at this code snippet that defines two command handlers for a server: import { plainToClass } from "class-transformer"; enum Command { COMMAND_1, COMMAND_2, } class Command1Data { foo1!: string } class Command2Data { foo2!: ...

What is the best way to retrieve the previously chosen item from an array?

I have successfully implemented dynamic pill tabs with one minor issue remaining. The most crucial aspect is that when I remove a pill, I want it to return to the previously opened tab. I have provided a StackBlitz example without routes on this page: -> ...

Using MongoDB Stitch with Angular 7 Development

Attempting to integrate MongoDB Stitch into my Angular 7 application has resulted in a failure with the following error: bson.browser.esm.js:453 Uncaught ReferenceError: global is not defined Version Angular 7.2.12 is being used and mongodb-stitch-brow ...

404 Error message encountered across all routes in a Node TypeScript Azure Function App - Resource not located

I keep encountering a 404 Not Found error on all routes while requesting my API. I am struggling to correctly write the code. Here's an example of my code: host.json: { "version": "2.0", "extensions": { & ...

Steps for incorporating universal style into Angular 6/7 library

I attempted to incorporate global styles in my Angular app similar to how it's done, but unfortunately, it didn't work as expected. The library I'm using is named example-lib. To include the styles, I added styles.css in the directory /proj ...

An effective way to define the type of a string property in a React component using Typescript

One of the challenges I'm facing is related to a React component that acts as an abstraction for text fields. <TextField label="Enter your user name" dataSource={vm} propertyName="username" disabled={vm.isSaving} /> In this set ...

Propagating numerical values through iterative iterations

I am currently facing an issue with passing values as props to a component using the forEach method in JavaScript. In addition to passing the existing values from an array, I also want to send another value that needs to be incremented by 1 for each iterat ...

Unexpected behavior observed in ng-select when pasting search values

When attempting to update an array of strings acting as the model for an ng-select, the values do not appear correctly in the search box. The values that are displaying correctly are the ones selected from the dropdown menu. However, the numbers I manuall ...

Should we implement REST API with authentication?

I am seeking guidance on building an application from scratch and I have encountered some challenges. The plan is to create a front-end using Angular and a backend that will communicate via REST API. This application will be deployed on individual devices, ...

What steps can I take to resolve the keyboard problem with Ionic Capacitor?

Currently, I am working on a straightforward Ionic Capacitor application. My objective was to display or hide the keyboard based on certain scenarios. Despite referring to the capacitor documentation for assistance with the keyboard functionality, I encoun ...

The addition operator cannot be used with the Number type and the value of 1

Encountering an issue where the operator '+' cannot be applied to types 'Number' and '1' buildQuerySpec() { return { PageSize: this.paging.PageCount, CurrentPage: this.paging.PageIndex + 1, MaxSize: '' ...

Looking to update the URL from another component?

My current project is using Angular 6 and I am working on creating a list of buttons on the left panel such as "Ice cream", "Pop corns", and more. The goal is for users to click on these buttons which will then change the URL of the add button located in t ...

What is causing VSCode's TypeScript checker to overlook these specific imported types?

Encountering a frustrating dilemma within VS Code while working on my Vite/Vue frontend project. It appears that certain types imported from my Node backend are unable to be located. Within my frontend: https://i.stack.imgur.com/NSTRM.png The presence o ...

Is there a method to improve type inference in vscode?

Recently, I created a component with a click handler that looks like this: onClick={(e) => { e.stopPropagation(); }} It seems clear to me, but then the Typescript compiler complains that it's not a valid signature for onClick, which actually a ...

tips for managing response time in firebase authentication state

I've been facing an issue with my web application in efficiently checking if it is firebase authenticated. The 'auth state object' doesn't seem to be functioning correctly on my template, as the expected sections are not appearing at al ...

Issue encountered: In Angular 8, an error is thrown stating "TypeError: Object(...) is not a function" when trying to utilize ng-idle/ng-keepalive within the eval

I've been attempting to incorporate ng-idle/ng-keepalive into my Angular 8 project, but no matter how many versions I install, the console keeps showing me this same error: Error: Uncaught (in promise): TypeError: Object(...) is not a function TypeEr ...

Resolving the non-null assertion error in TypeScript and React: A step-by-step guide

My code snippet is as follows: type ItemProps = { status?: string; } <Items status={status!} /> // encountering an error with a warning about forbidden non-null assertion // @typescript-eslint/no-non- ...

Enhance the visual appeal of your checkboxes in React Fluent UI by customizing the color of the checked mark and

I have a React application using Fluent UI. Currently, the <Checkbox/> component is displaying with its default colors and behavior like this: I want to customize the color of the checked mark and label (Green for checked mark and brown for label). ...