Archit Sandesara
Archit Sandesara

Reputation: 655

Value for message cannot be cast from ReadableNativeMap to String

I am new to React Native and in my React Native application, I am trying to display react-native Alert.alert() and for the message string, I am iterating over the JSON array and appending to message.

However, I am getting Value for the message cannot be cast from ReadableNativeMap to String error.

enter image description here

var message = ``;

if (responseJson.data.createdList) {
    message = `${responseJson.data.createdList.length} barcodes scanned :\n`

    responseJson.data.createdList.map(value => {
        message = message+  `${value} \n`
    })
}

if (responseJson.data.totalProcessed && responseJson.data.existList && responseJson.data.totalCreated !== responseJson.data.totalProcessed) {
    message = message + `${responseJson.data.existList.length} barcode already exists\n`
    responseJson.data.existList.map(value => {
        message = message + `${value} \n`
    })
}

message=message+  `Continue Scanning ${this.state.selectedProductName} ?`

Alert.alert(
    `Process Results - Total Created: ${responseJson.data.totalCreated}`,   message,
    [{
        text: "OK",
        onPress: () => {  },
    }]
);

I tried using JSON.stringify(message) instead of the message but it gives the same error. How can I use message in Alert.alert?

Upvotes: 0

Views: 4400

Answers (1)

Archit Sandesara
Archit Sandesara

Reputation: 655

I solved by using message = message.concat(" ", stringToAppend) instead of message= message+ stringToAppend.

Here's my updated code.

                               var message = "";
                               if (responseJson.data.createdList) {
                                   message = responseJson.data.createdList.length+ "barcodes scanned :\n"

                                   responseJson.data.createdList.map(value => {
                                       message = message.concat(" ", value + " \n");
                                   })
                               }
                               if (responseJson.data.totalProcessed && responseJson.data.existList && responseJson.data.totalCreated !== responseJson.data.totalProcessed) {
                                   message = message.concat(" ", responseJson.data.existList.length + " barcode already exists\n");
                                   responseJson.data.existList.map(value => {
                                       message = message.concat(" ", value + " \n");
                                   })
                               }
                               message = message.concat(" ", "Continue Scanning " + this.state.selectedProductName + " ?");

                               Alert.alert(
                                   `Process Results - Total Created: ${
                                   responseJson.data.totalCreated
                                   }`,
                                   message,
                                   [
                                   {
                                       text: "OK",
                                       onPress: () => {  },
                               
                                       }
                                   },
                               ]
                           );

reference: https://reactnativecode.com/combine-two-strings/

Upvotes: 1

Related Questions