-1

I am trying to add an id as an object property but i must be missing something important.

function appendData(products) {
var i = 0;
var productsPromises = products.map(function(product) {
    i++;
    var title = product['title'];
    var stock = product['stock'];
    var price = product['price'];
    var write = {Titulo: title, i: {Stock: stock, Price: price, date: date}};
    var readFiles = fs.readFile('./XXXXX/' + i, 'utf8', function(err, data){
        if (err) {
            console.log(err);
        }
        console.log(data);
    });


});
return Promise.all(productsPromises);
};
Mimetix
  • 272
  • 1
  • 4
  • 12
  • `Promise.all` does expect an array of *promises* as its argument. Your mapping function doesn't even return anything. – Bergi Aug 14 '14 at 14:59
  • Where are you actually *using* the `write` value in your code? – Bergi Aug 14 '14 at 15:00

1 Answers1

1

You can't do that in a JavaScript object literal. Create the object and then add the property as a separate statement:

var write = {Titulo: title };
write[i] = {Stock: stock, Price: price, date: date};

Also note that .map() will pass your callback function the array index as the second parameter, so you don't have to maintain your own counter.

Pointy
  • 405,095
  • 59
  • 585
  • 614