ShopifySharp icon indicating copy to clipboard operation
ShopifySharp copied to clipboard

Error when creating new product

Open flgatormike opened this issue 3 years ago • 22 comments

This library is great. I can get products, orders, customers, and even update a product, but when I try to create a product using CreateAsync, I get: "Object reference not set to an instance of an object" Any idea what the problem might be? Thanks!

flgatormike avatar Mar 04 '22 00:03 flgatormike

Thanks for the bug report @flgatormike! This package has unit tests that run with every new release, including ones for creating a new product, and they don't seem to have this issue. Could you possibly post the full exception with stack trace so I can take a look? It might also help if you can post the code you're using to create a product.

nozzlegear avatar Mar 09 '22 04:03 nozzlegear

Thanks for the reply! I am trying to create a product by getting a product and just changing the id and title and then creating the product. I have also tried manually building the product. The stack trace is below but it looks like it doesn't step into the relevant code. Any idea what I might be doing wrong?

            var product = await GetProduct(7550715560151);
            product.Id = 7550715560133;
            product.Title = "TEST";
            product = await service.CreateAsync(product);

at ShopifySharp.ShopifyService.<>c__DisplayClass27_01.<<ExecuteRequestAsync>b__0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.DefaultRequestExecutionPolicy.<Run>d__01.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.ShopifyService.<ExecuteRequestAsync>d__271.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at ShopifySharp.ProductService.<CreateAsync>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at qb_shopify.Main.<CreateProducts>d__22.MoveNext() in C:_projects\emustiga\qb-shopify\Main.cs:line 368

flgatormike avatar Mar 14 '22 00:03 flgatormike

I also just tried copying the code in the unit test and get the same exception:

    public async Task<Product> CreateTestProduct(ProductCreateOptions options = null)
    {
        var service = new ProductService(domain, accessToken);
        var obj = await service.CreateAsync(new Product()
        {
            Title = "Test Product",
            Vendor = "Test Vendor",
            BodyHtml = "<p>Test Product</p>",
            ProductType = "Product",
            Handle = Guid.NewGuid().ToString(),
            Images = new List<ProductImage>
            {
                new ProductImage
                {
                    Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
                }
            },
        }, options);

        return obj;
    }

flgatormike avatar Mar 14 '22 00:03 flgatormike

Hmm you're right, that stack trace doesn't really give us anything to go on. Let's take another approach: when ShopifySharp throws an exception, the exception will have a property on it named RawBody. This is the exact string that Shopify is replying with, it may contain an error message or at least give us something else to look into. Can you wrap your code in a try/catch and then log that RawBody property to post here?

try 
{
    product = await service.CreateAsync(product);
}
catch (ShopifySharp.ShopifyException ex)
{
	Console.WriteLine(ex.RawBody);
	throw;
}

nozzlegear avatar Mar 14 '22 18:03 nozzlegear

Hmm, just tested and the ShopifyException isn't thrown, just the standard Exception. Strange that I am able to list orders, products, customers, and even update a product, but only get this error with create.

flgatormike avatar Mar 14 '22 19:03 flgatormike

Ah that's interesting, so it's unlikely to be something related to Shopify's response itself unless it's simply not returning the expected JSON. I think our best bet at the moment is for you to clone this project and include it in your own project directly and see what happens when you debug it. That should at least point you to the source of the problem and hopefully I'll be able to write a fix for it.

Let me know if you need any help cloning ShopifySharp and including it in your project!

nozzlegear avatar Mar 14 '22 21:03 nozzlegear

Something else to try: I know you've copied the code from the test project and had it break there too, but in the other sample code you provided above it looks like you've got a method named GetProduct and you're using that base product data to create a new one. What happens if you use the productService.GetAsync instead of your custom method? Does that throw the same error when you try to create with it?

var product = await service.GetAsync(7550715560151);
product.Id = 7550715560133;
product.Title = "TEST";
product = await service.CreateAsync(product);

nozzlegear avatar Mar 14 '22 21:03 nozzlegear

so here is where I am getting the error. rawResult looks like it is valid json, but data is null. It looks like the root element inside rawResult is "products" but rootElement is set to "product" (without the s) which looks like the cause of the error. Where would this be configured?

image

flgatormike avatar Mar 14 '22 22:03 flgatormike

Thanks! So it is the response json causing the problem. Could you post that raw result value here? I’ll run it in a unit test and see what happens.

-- Josh Harms

On Mon, Mar 14, 2022, at 17:12, flgatormike wrote:

so here is where I am getting the error. rawResult looks like it is valid json, but I am getting an error on deserialization: image https://user-images.githubusercontent.com/47644612/158269343-df36b5d2-edb5-46a5-852d-772b96d8038c.png

— Reply to this email directly, view it on GitHub https://github.com/nozzlegear/ShopifySharp/issues/723#issuecomment-1067352986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASOE7HFWIK3AVNAJO7NSNLU762TTANCNFSM5P4DLBGA. You are receiving this because you commented.Message ID: @.***>

nozzlegear avatar Mar 14 '22 23:03 nozzlegear

Yes, the problem in the json is the root element. The root of the json is "product" but the code is looking for "products" with an s.. not sure if this mismatch is a problem with the shopify store or my code?

flgatormike avatar Mar 15 '22 00:03 flgatormike

That would definitely be the source of the problem. The ProductService.CreateAsync method should be using the string "product" as the root. I'm unable to locate any point where it was using "products" in the git history, but I might just be overlooking it. Are you overriding the method at all with your own implementation? If not, have you tried updating to the latest version of ShopifySharp to see if that fixes the problem?

nozzlegear avatar Mar 15 '22 01:03 nozzlegear

"products" (with the s) is coming back from shopify, so maybe this is a problem with the store configuration?

flgatormike avatar Mar 15 '22 01:03 flgatormike

Below is the code I am using to create a test product, which makes a call to executerequestasync which returns the json below which is a list of products and that is where I am getting the error. Not sure why I am getting a list of products here, and that is where I am getting the error...

            var product = await service.CreateAsync(new Product()
            {
                Title = "Test Product",
                Vendor = "Test Vendor",
                BodyHtml = "<p>Test Product</p>",
                ProductType = "Product",
                Handle = Guid.NewGuid().ToString(),
                Images = new List<ProductImage>
                {
                    new ProductImage
                    {
                        Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
                    }
                },
            }, options);

{"products":[{"id":7553612677335,"title":"Case OPC-8301-3","body_html"]}}

flgatormike avatar Mar 16 '22 01:03 flgatormike

When I get orders, I get a root element of "orders" When I get products, I get a root element of "products" When I get customers, I get a root element of "customers" When I update a product, I get a root element of "product" And those all work fine But when I create a product, I get a root element of "products" when it should be "product" Any ideas?

flgatormike avatar Mar 17 '22 17:03 flgatormike

Honestly it's very strange behavior. Just to confirm, which version of ShopifySharp are you using? And have you extended the ProductService with your own class/override methods at all?

nozzlegear avatar Mar 21 '22 04:03 nozzlegear

Honestly it's very strange behavior. Just to confirm, which version of ShopifySharp are you using? And have you extended the ProductService with your own class/override methods at all?

No I haven't extended it. The store owner says it is "Store 2.0", could it be something with the way the store was configured?

flgatormike avatar Mar 21 '22 14:03 flgatormike

Thanks! As far as I know that shouldn't affect anything unless it uses a different version of the API. Which version of ShopifySharp do you have installed in your project? Once we know that we'll know which version of Shopify's API is being used by the package.

-- Josh Harms

On Mon, Mar 21, 2022, at 09:09, flgatormike wrote:

Honestly it's very strange behavior. Just to confirm, which version of ShopifySharp are you using? And have you extended the ProductService with your own class/override methods at all?

No I haven't extended it. The store owner says it is "Store 2.0", could it be something with the way the store was configured?

— Reply to this email directly, view it on GitHub https://github.com/nozzlegear/ShopifySharp/issues/723#issuecomment-1073944013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASOE7ECT2E6F5IWHIF3RQLVBB7IJANCNFSM5P4DLBGA. You are receiving this because you commented.Message ID: @.***>

nozzlegear avatar Mar 21 '22 14:03 nozzlegear

So when I call the different ProductService methods, I took a look at the url that is being called in ExecuteRequestAsync. They are all fine except for CreateAsync which is posting to products.json.. the content looks fine, it is posting the product data but the uri doesn't look right. Not sure where that is being set?

ProductService > ListAsync root element: products method: GET uri: https://mydomain/admin/api/2021-10/products.json

ProductService > GetProduct root element: product method: GET uri: https://mydomain/admin/api/2021-10/products/7550715560151.json

ProductService > UpdateAsync root element: product method: PUT uri: https://mydomain/admin/api/2021-10/products/7550715560151.json

ProductService > CreateAsync root element: product method: POST uri: https://mydomain/admin/api/2021-10/products.json

flgatormike avatar Mar 21 '22 20:03 flgatormike

I guess that endpoint is correct for creating a new product, however mine is 2021-10 and the one in the shopify documentation is 2022-01, just a difference of 3 months, that couldn't be my problem could it?

image

flgatormike avatar Mar 22 '22 01:03 flgatormike

That shouldn't be an issue, the 2021-10 version we're using right now is still supported until October 2022. I just had a thought though! In the email you sent me earlier today, the shop website you're using looks like the "real" domain but it's not the shop's *.myshopify.com domain.

I think Shopify is redirecting the request to the shop's *.myshopify.com domain by returning a 301 or 302 redirect. The HttpClient might be following that redirect but not preserving the POST method, instead turning it into a GET request which is why Shopify would return a list of products (because listing products and creating a new product both use the same products.json endpoint).

Can you try changing your code to use your shop's *.myshopify.com domain?

var service = new ProductService("example.myshopify.com", accessToken);

-- Josh Harms

On Mon, Mar 21, 2022, at 20:03, flgatormike wrote:

I guess that endpoint is correct for creating a new product, however mine is 2021-10 and the one in the shopify documentation is 2022-01, just a difference of 3 months, that couldn't be my problem could it?

image https://user-images.githubusercontent.com/47644612/159387091-0aa973ff-8118-4cfb-99e4-da67e247bb98.png

— Reply to this email directly, view it on GitHub https://github.com/nozzlegear/ShopifySharp/issues/723#issuecomment-1074573138, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASOE7AYTHWDG5HWYUMYLD3VBEL7RANCNFSM5P4DLBGA. You are receiving this because you commented.Message ID: @.***>

nozzlegear avatar Mar 22 '22 01:03 nozzlegear

Hey, that worked! Thanks so much! It is strange that the update PUT worked with the other domain but the create POST didn't...

flgatormike avatar Mar 22 '22 15:03 flgatormike

Awesome, glad to hear it worked! I’m not sure why the put request didn’t break like the post request did, but I think I’ll modify this package to make sure it doesn’t follow redirects in the future. That should help pinpoint this kind of problem more quickly.

Thanks for your patience and for working with me while we debug the problem!

-- Josh Harms

On Tue, Mar 22, 2022, at 10:05, flgatormike wrote:

Hey, that worked! Thanks so much! It is strange that the update PUT worked with the other domain but the create POST didn't...

— Reply to this email directly, view it on GitHub https://github.com/nozzlegear/ShopifySharp/issues/723#issuecomment-1075298084, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASOE7GGY7HXRIMW6AJ2SBLVBHOVLANCNFSM5P4DLBGA. You are receiving this because you commented.Message ID: @.***>

nozzlegear avatar Mar 22 '22 15:03 nozzlegear

A little note for myself here, I've added an FAQ to the readme that mentions these domain problems.

nozzlegear avatar Sep 20 '22 21:09 nozzlegear

I am having a very similar issue, though I have made sure to use *.myshopify.com as the domain. When I try to create a product, I receive back an 'Object reference not set to an instance of an object.' that is being thrown at {T DeserializeWithNewtonsoft[T](System.String, System.String, System.Nullable`1[Newtonsoft.Json.DateParseHandling])}. GETing the products works correctly. This is the first time I have tried to create anything (POST for order, customer, product, etc.). Any ideas?

pmartinciibo avatar Dec 08 '23 03:12 pmartinciibo

@pmartinciibo It sounds like that could be something slightly different then, especially since it's pointing to our special date time serialization attribute. Could you create a new issue for your error, and include the full error message, stack trace and example code if possible?

nozzlegear avatar Dec 08 '23 04:12 nozzlegear