Jay Fielding
Jay Fielding

Reputation: 33

Unable to call `++` with (`String`, `Array<String>`)

I'm trying to format a phone number like phoneNumber: "+1" ++ payload.phoneNumber and I'm getting an error cannot coerce an Array to String. Why does my output think I have an Array and not just a String?

The error message I'm getting is... "Unable to call ++ with (String, Array<String>): - Expecting Type: Array<S>, but got: String."

Upvotes: 3

Views: 1801

Answers (3)

AndyDaSilva52
AndyDaSilva52

Reputation: 159

One thing to add

This error could happen when there is a mistake in the target expression

%dw 2.0
output application/json
---
payload map (payload01, indexOfPayload01 ) -> {
    phoneNumber: ( (payload.phoneNumber default "") )
}

So to fix make sure you're declaring correct

payload map (payload01, indexOfPayload01 ) -> {
    phoneNumber: ( (payload01.phoneNumber default "") )
}

Upvotes: 0

Siva
Siva

Reputation: 98

You can use dataweave joinBy() where payload.phonenumber is an array [1,2,3,4]

phoneNumber: ("+1" ++ joinBy(payload.phoneNumber,"") as String)

Upvotes: 0

machaval
machaval

Reputation: 5059

The problem here is that according to the metadata payload.phoneNumber is an array. This can be either because payload is an Array and then payload.phoneNumber will return all the phone numbers of all the elements payload or because the phoneNumber is an Array. So most probably you want to do a map or just pick the first element.

Upvotes: 6

Related Questions