1

I have a simple $resource factory.

.factory('Order', order)

order.$inject = ['$resource', "ApiEndpoint", "UserRecord"];

function order($resource, ApiEndpoint, UserRecord) {
  return $resource(ApiEndpoint.url + 'orders.json', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'}
  });
}

Here is the code I'm executing.

var params = {product_id: 32342, variant_id: 536341};

Order.create(params, function( resp ) {
  console.log("success");
});

When I run create the params are not going through. No params are passing. Why is this and how can I fix this?

7
  • Any errors in console?
    – leo.fcx
    Jul 28, 2015 at 18:49
  • None. No errors at all. Well besides the one from the server that states no params were sent.
    – thank_you
    Jul 28, 2015 at 18:50
  • Does the server respond at all? What appears in the network tab? What's the response?
    – Joseph
    Jul 28, 2015 at 18:58
  • The response is a 200. The issue is that the server doesn't receive the params.
    – thank_you
    Jul 28, 2015 at 19:01
  • Apologizes. It doesn't receive any of the params. It's as if no params were sent.
    – thank_you
    Jul 28, 2015 at 19:03

2 Answers 2

2

According to the docs:

The action methods on the class object or instance object can be invoked with the following parameters:

HTTP GET "class" actions: Resource.action([parameters], [success], [error])
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])

So you could try:

var params = {product_id: 32342, variant_id: 536341};

Order.create({},params, function( resp ) {
  console.log("success");
});

The first parameter is used to fill in the parameters of the url (e.g /foo/:id would require {id: 324} as the first parameter)

Update:

Got the order of parameters incorrect:

var params = {product_id: 32342, variant_id: 536341};

Order.create(params,{}, function( resp ) {
  console.log("success");
});
2
  • Hmm. I attempted this but the same result is produced. The params are still not going through.
    – thank_you
    Jul 28, 2015 at 19:10
  • I just fixed this. You were close. Just had to swap out the first two arguments.
    – thank_you
    Jul 28, 2015 at 19:18
-1

I realized I'm missing a second argument, POST-DATA.

Order.create(params, {}, function( resp ) {
  console.log("success");
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.