Alfonso Alfonso - 1 year ago 67
Javascript Question

Unexpected token square bracket in Nodejs

Here is a simple example:

for (i = 1; i < totalRows; i++) {
if(rows[i][10] == null) {
spreadsheet.add({ [i]: { 10: "Added" } });

why do I get an error "Unexpected token [" here:

spreadsheet.add({ [i] : { 10:

I am trying to run the app on
I cant update it as its a free server provided by OpenShift.
It runs fine on my
computer with
nodejs v4.4.7

Please advice a workaround for this code, so it could be executed.

Basically what this code does is, it looks in every row if
column 10 is empty
, and if its empty, it adds Added

Answer Source

You defining an object property invalidly.

This is the code that will work:

for (i = 1; i < totalRows; i++) {
    if(rows[i][10] == null)  {
      var obj = {};
      obj[i] = { 10: "Added" };

Check MDN documentation regarding the working with properties:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download