0

this is really confusing me and I'm not even sure how to debug it. Basically what is happening is I am running this query:

ts file:

this.httpService.getLastModified({ internalId: this.companyID }).subscribe((res) => {
if (res && res[0].lastModified) {

  // code to do inside if (update the lastModified)

} else {

  // code to do inside else (use a last modified date as the beginning of last year and then set it to now)

}

service file:

getLastModified(body): Observable<Cases> {
  return this.http.post('/db/cases/lastModified/', body);
}

routes:

router.post('/lastModified/', (req, res) => {
  const { body } = req;
  Companies.find({ internalId: body.internalId }, (err, data) => {
    res.json(data);
  });
});

database:

enter image description here

So the very first time I run the code I expect to go into the else section and inside there it will use the date as the beginning of last year and then set the lastModified (now) in the database. From then onwards it should always be in the if section.

Now the strange part. On the first load of the day, the code runs the else statement, even though there is a lastModified value there. It is returning only the id of the object. However from then on it works fine all day, until the next day again the first load -- this carries on like this always.

This is driving me crazy as Im waiting a day just to debug and i only get one shot at it.

Does anyone have any idea why it is failing or come across anything like this?

AskYous
  • 4,332
  • 9
  • 46
  • 82
Andrew
  • 227
  • 1
  • 5
  • 16
  • *first time I run the code I expect to go into the else section* .... *first load of the day, the code runs the else statement* ... so far the code is doing what you expect – Jaromanda X Sep 16 '20 at 08:27
  • That is for the very first load -- so there is no lastModified present and so it goes into the else to set it. – Andrew Sep 16 '20 at 08:29
  • Have you tried to use `BehaviorObject` instead of `Observable`? – ivangreek Sep 16 '20 at 08:29
  • @ivangreek No I haven't. Not actually familiar with that but will look into it. Hopefully it will be able to help. – Andrew Sep 16 '20 at 08:32
  • @Andrew I had a typo in my previous comment. It is `BehaviorSubject`. Please, send feedback if it solve your problem. Here is a post about [BehaviorSubject and Observable](https://stackoverflow.com/questions/39494058/behaviorsubject-vs-observable) – ivangreek Sep 16 '20 at 09:17
  • @ivangreek thank you for that. I think I need to update the question - on the very first run I want it to use a date of start of last year. Then from then on use the lastModified. So what would i have as the initial value for the behaviour subject in that case? - I have edited the question to reflect this. – Andrew Sep 16 '20 at 09:27

0 Answers0