0

So before save I intend to get details of an object and use them in an email that is sent from an app to the customer after they have paid for items.

I have an array column in my database that stores strings as objects in the array. Each string object is a bunch of details about a particular item of an order put into one string.

This is how I access the array:

order.get('fullOrderDetails', [0, 1])

This is fine if my array will always have 2 objects but in this case the amount of objects will always vary.

If you look at my code below I have a for loop that loops through objects in the array. I need to some how do this: order.get('fullOrderDetails', [index of objects separated by commas]) dynamically.

Parse.Cloud.beforeSave("Dispatch", function(request, response) {

    // email
    var order = request.object;

    mandrill.initialize("API-KEY");

    var arrayLength = order.get('fullOrderDetails').length;
    for (var i = 0; i < arrayLength; i++) {


    }

    mandrill.sendEmail({
        message: {
          text: "Your order: " + order.get('fullOrderDetails', [0, 1]) + "has been processed and we will notify you when your order has been dispatched!",
          subject: "Thank you for your order!",
          from_email: "[email protected]",
          from_name: "aStore.com!",
          to: [
            {
              email: order.get('email'),
              name: order.get('name')
            }
          ]
        },
        async: true
      }, {
        success: function(httpResponse) { response.success(); },
        error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
      });
});

So calling my array objects manually using index numbers is not ideal for me because the amount of objects vary so I need a way to do it dynamically.

How do I do this?

Thanks for your time.

2
  • this is confusing. do you always need a list of zero-n? why can't you pass the data itself as an array instead of an index? Commented Aug 12, 2014 at 22:11
  • @dandavis Do I do it like this order.get('fullOrderDetails')? If so will it pull out all data automatically? As you can see I'm calling it in a string. Each needs to be on a new line. Commented Aug 12, 2014 at 22:18

1 Answer 1

1

If i understand you correctly you just need to dynamically build an array based on how many items were ordered, with each index

[0,1,2,3,4...]

In which case all you need is the one line below, and then to pass this new arr to your order.get call.

var arr=[], arrayLength = order.get('fullOrderDetails').length;
// this will make the arr -->[0,1,2.. ] 
for (var i = 0; i < arrayLength; i++) {
   arr.push(i);
}

// i pass it in below

mandrill.sendEmail({
    message: {
      text: "Your order: " + order.get('fullOrderDetails', arr) + "has been processed and we will notify you when your order has been dispatched!",
      subject: "Thank you for your order!",
      from_email: "[email protected]",
      from_name: "aStore.com!",
      to: [
        {
          email: order.get('email'),
          name: order.get('name')
        }
      ]
    },
    async: true
  }, {
    success: function(httpResponse) { response.success(); },
    error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
  });
Sign up to request clarification or add additional context in comments.

2 Comments

I you want to list each entry on a different line then all we need to do is build the string in the loop, moving the order.get into the loop and construction the string. Then we pass the string to the mandrill.sendEmail call. If you can confirm how you want it done i can change the code.
This worked. I'm going to need to look into using html in this code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.