1

I have a angular 8 app that I would like to build and tried to run "ng build --prod --aot", files are minified and uglified, it also shows index page but when I try to login, the services needed to login throws ". is not a function or undefined" first page of app

Note: ng build is fine, css is fine, I look at sources and js is loaded but it still throws undefined error

Spatz
  • 15,734
  • 6
  • 54
  • 57
just a dev
  • 11
  • 3
  • in your template or code you are using logout property with a dot somewhere like obj.logout? – Aakash Garg Jun 12 '20 at 08:17
  • yes along with that and more importantly, the function associated with login is not being fired because it is tagged as undefined, that is the getUser function even though it is minified, uglified and loaded in bundle in index.html – just a dev Jun 12 '20 at 08:21
  • show us some code. – Aakash Garg Jun 12 '20 at 08:22
  • it even locates exactly where in code the said undefined function is when I click to it in console. I have been looking for answers and the same question here but nothing shows up and the closes to it is this https://stackoverflow.com/questions/37558656/angular-cli-ng-build-doesnt-produce-a-working-project which I also tried the solution but to no avail, it just points to right url of app. Again the app is fine yet the functions don't fire – just a dev Jun 12 '20 at 08:26
  • https://imgur.com/iqyBV0R the highlighted is the function. it points to a function in a service. that is fine and firing in development. I'm sorry I don't know what I should show you. perhaps the angular.json? – just a dev Jun 12 '20 at 08:33
  • nope, first you don't need that self = this and using self inside arrow functions. in error click on line on which it shows error and show us that line. – Aakash Garg Jun 12 '20 at 08:37
  • https://imgur.com/IDn9IGK this is the signin function these are pictures of angular.json https://imgur.com/vK0Kuvq https://imgur.com/cvXDH9c https://imgur.com/oBeANcH https://imgur.com/Ldf5Opj Sorry I am new here in asking questions and am desperate enough to post my own question as I have not seen same problem with me. – just a dev Jun 12 '20 at 08:43
  • you are using .logout some where where the calling object is null. – Aakash Garg Jun 12 '20 at 08:44
  • https://imgur.com/V1QrMwx the highlighted is the calling object, is my service for holding all database related functions. getUser is a Promise and is working fine in development build. I do not know why it is acting up in production. Sorry for having noob mistake if there is any. I want to learn better. – just a dev Jun 12 '20 at 08:52
  • https://imgur.com/pOjdvYG this is the error it is coming from in production build – just a dev Jun 12 '20 at 08:57
  • click on pretty print and then see where error is coming. – Aakash Garg Jun 12 '20 at 09:03
  • using logout somewhere in template? – Aakash Garg Jun 12 '20 at 09:03
  • Yes, that is auto logout when at least 1 requirement is not met and then I just reload the page. But yes it seems the service is having errors since both getUser and logout is in same service? But I do not know how it is harboring errors. Maybe there is something wrong with the build https://imgur.com/3Agen7X this is the pretty print – just a dev Jun 12 '20 at 09:11
  • you have @Injectable{'providedIn': root} on top of dataservice? – Aakash Garg Jun 12 '20 at 09:15
  • omg yes? why? what does it mean? – just a dev Jun 12 '20 at 09:16
  • that is fine. you are using somewhere .logout in template? then replace it with ?.logout – Aakash Garg Jun 12 '20 at 09:18
  • I use it with this.dataServ.logout do you mean i exclude the 'this'? like this: dataServ.logout only? – just a dev Jun 12 '20 at 09:19
  • no, in the html you are using logout somewhere? – Aakash Garg Jun 12 '20 at 09:20
  • no I do not. it is fired immediately in an observable upon init. only in component.ts I use logout – just a dev Jun 12 '20 at 09:22
  • you are having dataservice in provider array somewhere? – Aakash Garg Jun 12 '20 at 09:24
  • I think I don't as I only inject services through component constructor. How should I do that? – just a dev Jun 12 '20 at 09:26
  • not sure then, what's the issue. – Aakash Garg Jun 12 '20 at 09:35
  • I added services providers in app.module will it do a difference? – just a dev Jun 12 '20 at 09:38
  • no shouldn't add it there. – Aakash Garg Jun 12 '20 at 09:41
  • do you need to see other code? – just a dev Jun 12 '20 at 09:56

1 Answers1

0

For anyone who also encountered this error, I have solved this error by omitting { providedIn: 'root' }

In @Injectable of my Service and Injected it to my app.module in providers array

It seems that because of ng build my service becomes undefined in production I don't know how but it worked when I omitted it.

just a dev
  • 11
  • 3