-2

I am trying to use InMemoryDbService as data source in Angular 14.0.2 (/Node 16.15.1) project. I'm getting "api/[something]" not found in the browser log, no other apparent error. I can't share the whole code but I'll try to replicate the relevant parts here.

app.module.ts

    (...)
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';

    (...)
  imports: [
    (...)
      HttpClientModule,
      HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {dataEncapsulation:false}),
    ThingsModule,
    AppRoutingModule
    (...)

in-memory-data.service.ts

import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { MockThings } from './thing/mock-thing-list';
import { Thing} from './thing/thing';

@Injectable({
  providedIn: 'root'
})
export class InMemoryDataService implements InMemoryDbService {

    createDb() {
        const Things: Thing[] = MockThings;
        return { Things};
    }

}

thing.service.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, Observable, of, tap } from 'rxjs';
import { Things} from './thing';

@Injectable()
export class ThingService {

    constructor(private http: HttpClient) { }

    getThingsList(): Observable<Thing[]> {
        return this.http.get<Thing[]>('api/things').pipe(
            tap((response) => this.log(response)),
            catchError((error) => this.handleError(error, []))
        );
    }

private log(response: Thing[] | Thing| undefined) {
    console.table(response);
}

private handleError(error: Error, errorValue: any) {
    console.error(error);
    return of(errorValue);
}


(...)
}

thing-list.component.ts

(...)

@Component({
  selector: 'app-thing-list',
  templateUrl: './thing-list.component.html',
})

export class ThingListComponent implements OnInit {
    Things: Thing[];
    selectedThing: Thing|undefined;

    constructor(private router: Router, private thingService: ThingService ) {

    }
    ngOnInit(): void {
        this.ThingService .getThingList().subscribe(thingList => this.Things);
    }

    (...)
}
8
  • angular 16 is not a thing Commented Jun 26, 2022 at 13:15
  • @R.Richards Sorry about that, what it Angular v16.15.1 supposed to be then? Commented Jun 26, 2022 at 13:22
  • 1
    Do you mean node v16.15.1 ? Commented Jun 26, 2022 at 13:27
  • Look in your package.json file to see what version of angular you are working with Commented Jun 26, 2022 at 13:28
  • 1
    The fact that a simple typo fix solved your problem means this question should remain closed. The close reason is wrong, out reopening would just lead to another round of votes to close as typo / unreproducible. Simple misspellings are unlikely to help future visitors, and thus are not worth keeping. Commented Jun 27, 2022 at 5:41

1 Answer 1

2

Shouldn't it be in camelCase? Things -> things

 const things: Thing[] = MockThings;
 return { things };
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, it looks like the URL is generated from the name of that variaible and it is case sensitive... using api/Things would also work (though I'd expect this one is not recommended). Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.