0

I'm developing a small typescript app and stuck in value assigning.

Here are interfaces.

export interface SearchTexts {
    SearchText: SearchText[];
}

export interface SearchText {
    Value: string;
    BusinessID: string;
}

Assign values like this.

let serchTextArr: SearchText[] = [];
const invoiceNos: string[] = ["INV4587965", "INV4589654"];

for (var idx in invoiceNos) {
  let searchText: SearchText = {
    Value: invoiceNos[idx],
    BusinessID: "BSD458"
  };    
  serchTextArr.push(searchText);
}

let searchTexts: SearchTexts;
searchTexts.SearchText = serchTextArr; //Getting error when assigning

Below is the error

(node:17661) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'SearchText' of undefined

I used console.log for displaying whether the serchTextArr doesn't have values or not. But it has values and still cant figure out the issue. Can anyone tell me where the issue is?

2
  • 2
    let searchTexts: SearchTexts = {SearchText: []}; Commented Oct 28, 2020 at 6:59
  • Thanks very much @ttquang1063750... you saved my time.... thanks again.... Commented Oct 28, 2020 at 7:24

1 Answer 1

1

As far as I can tell, you've tried to access an object called searchTexts which you haven't defined (or shown that you have). Which would explain the error message:

cannot read SearchText of undefined

The simple solution to this to declare the variable like how @ttquang1063750 says to do it:

const searchTexts: SearchTexts = {SearchText: []};

Just outside your for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

No prob! glad to help out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.