cart.addProduct Returning Error



Seems to be a theme, but sorry … new to Moltin as well. Trying to get the basics of the Swift tutorials, so I created a single view swift project using xCode 9.3. Installed the pod for Moltin 3.0.7. Just to get see this work, I put the following code into the single view …

override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        let moltin: Moltin = Moltin(withClientID: "XXXX", withLocale: Locale(identifier: "en_US"))
        let cartID = UUID().uuidString
        moltin.cart.addProduct(withID: "XXXX",
                               ofQuantity: 1,
                               toCart: cartID) { result in
                                switch result {
                                case .success:
                                    print("Added a product to the cart")
                                case .failure(let error):
                                    print("Got error adding a product to the cart: \(error)")

When I try to add a product to the cart, I get the failure case with the message …

Got error adding a product to the cart: couldNotParseData(Optional(Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: “Expected to decode Array but found a dictionary instead.”, underlyingError: nil))))

I have used the products.all function to check that my product IDs are correct, I have also used the cart.items function after the cart.addProduct to see if the product got added either way, but the cart items count is still zero.

Any insights? Thanks.


Hey George,

That awesome to hear you are working with the new swift SDK. We have taken a quick look at this. One quick question for you, did you replace “xxx” with the product id you want to add to cart? We were able to re create the error if the placeholder for product id is left in the call. (withID: “XXXX”. per your code block above) We did not have any issues when we added in a product.

        moltin.cart.addProduct(withID: (self.product?.id) ?? "",
                           ofQuantity: 1,
                           toCart: AppDelegate.cartID) { result in
                            switch result {
                            case .success:
                                print("Added a product to the cart")
                            case .failure(let error):
                                print("Got error adding a product to the cart: \(error)")

Let me know if that is the case, if not we will keep working to get everything going for you.


I used a product ID from the Dashboard as a literal string … (e.g. "moltin.cart.addProduct(withID: “607ad517-f023-47b6-9a11-471228829a86”, …), not from the product object. Could that be the difference?


Could you use the id from the product object


Sure, let me give it a try and see what happens.


Bingo … that did the trick.

Suggestion … I already know the ID from the dashboard, so rather than query first, then use the object, which takes an extra round trip that is unneeded, can I request that we be able to use the literal value? Thanks.


Great, I will look into that. Certainly let me know if you run into anything else.



The original “couldNotParseData” error was due to the SDK not parsing the error JSON that the API returned correctly. I’ve just released 3.0.8 of the SDK - this fixes that, and will let you now know what the error the API was returning was.

Hope this helps you!


Thanks, appreciated. I will take a look.


Thanks for the update. The 3.0.8 version gave me a very descriptive message for what was wrong, thanks for the help.


Fantastic! Hope everything goes well!


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.