0

Trying to get a basic model of my functions working.

Frontend (Angular): the body data will be JSON of this class:

class BackendParams {
  listValues: any;

  constructor( netList: any ) {
    this.listValues = netList;
  }
}

Then a function creates the class object:

const params = new BackendParams(list);

then calls a (still in the front-end) Angular function to send it to the backend:

  onClickTest(params: any) {
    const A = 1;
    const B = 2;
    const NameString = 'test';
    const formData = new FormData();
    formData.append('NetworkList', JSON.stringify(params));
    let url = `${this.url}/CalibrationModel/1/2/SampleTest/TestModel`;

    this.http.post(url, formData).subscribe(  
          (data) => { 
            console.log(data);
          });
  }

BACKEND:

class BackendParams
{
    List<Constituent> listNetworkConstituents;

}

The following is image of the source code so you can see the syntax red underlines Note 2 syntax errors

I don't think the two are related (or are they?) but referencing the body parameters is of course essential.

And, of course, let me know anything else you see that might be a problem.

Thanks for your help. I learn a lot from you guys. Yogi

Yogi Bear
  • 943
  • 2
  • 16
  • 32
  • These look like type conversion errors to me - your return type is Task<> but you are trying to return a boolean. You only need to use Task with an async method so there's no need for it here. Change the return type to boolean to fix that one. – JohnD91 Dec 12 '19 at 17:04
  • Hi John, I'm still confused. The code above says Task, right so shouldn't that mean the return of a bool 'x' should work? OR are you saying I'm not returning a TASK of boolean type? – Yogi Bear Dec 12 '19 at 17:08
  • You aren't returning a Task of boolean type, but there is no need for it anyways because you aren't calling anything asynchronously. Tbh I'm not really sure why that boolean is even there - it's just set to true and returned no matter what happens in the rest of your code. – JohnD91 Dec 12 '19 at 17:14

1 Answers1

1

If your method were marked as async then returning a bool would work. So public async Task<bool>..., but that isn't the case. However, as @JohnD91 said, if you're not using await in your method, it doesn't need to be async and it also doesn't need to return a Task.

The other problem is that parmsJSON is misspelled, because it's defined in the method signature as paramsJSON. You're missing the other a.

Matt U
  • 4,970
  • 9
  • 28
  • Hi Matt. I'm glad the whole thing wasn't just a mispelling... LOL thanks. Now for the async. If I understand correctly, I must have 'async' in order to return a value... and I see now (having tried this) you are absolutely correct. I'm marking your answer correct, but if you care to comment further, is there a way to pass a return type FROM a Task without marking async, since async insists that the function contain an await-able internal function. Thanks again. – Yogi Bear Dec 12 '19 at 17:16
  • You can `return Task.FromResult(x)`. But then the type returned from the method is not simply `bool` , it's `Task`. – Matt U Dec 12 '19 at 17:19