Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when creating new product #723

Closed
flgatormike opened this issue Mar 4, 2022 · 25 comments
Closed

Error when creating new product #723

flgatormike opened this issue Mar 4, 2022 · 25 comments
Labels

Comments

@flgatormike
Copy link

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!

@nozzlegear
Copy link
Owner

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 nozzlegear added the bug label Mar 9, 2022
@flgatormike
Copy link
Author

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.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.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.d__22.MoveNext() in C:_projects\emustiga\qb-shopify\Main.cs:line 368

@flgatormike
Copy link
Author

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;
    }

@nozzlegear
Copy link
Owner

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;
}

@flgatormike
Copy link
Author

flgatormike commented Mar 14, 2022

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.

@nozzlegear
Copy link
Owner

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
Copy link
Owner

nozzlegear commented Mar 14, 2022

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);

@flgatormike
Copy link
Author

flgatormike commented Mar 14, 2022

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

@nozzlegear
Copy link
Owner

nozzlegear commented Mar 14, 2022 via email

@flgatormike
Copy link
Author

flgatormike commented Mar 15, 2022

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?

@nozzlegear
Copy link
Owner

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?

@flgatormike
Copy link
Author

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

@flgatormike
Copy link
Author

flgatormike commented Mar 16, 2022

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
Copy link
Author

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?

@nozzlegear
Copy link
Owner

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?

@flgatormike
Copy link
Author

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?

@nozzlegear
Copy link
Owner

nozzlegear commented Mar 21, 2022 via email

@flgatormike
Copy link
Author

flgatormike commented Mar 21, 2022

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
Copy link
Author

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

@nozzlegear
Copy link
Owner

nozzlegear commented Mar 22, 2022 via email

@flgatormike
Copy link
Author

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

@nozzlegear
Copy link
Owner

nozzlegear commented Mar 22, 2022 via email

@nozzlegear
Copy link
Owner

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

@pmartinciibo
Copy link

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?

@nozzlegear
Copy link
Owner

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants