5

I'm looking to use the sails attribute type 'array' in my app, but I can't find documentation for this anywhere.

I'd like to do the following:

module.exports = {
 attributes : {
  images: {
   type: ['string']
  },
  location: {
   type: ['float','float']
  }
 }
}

image is an array that will hold a list of image urls and location will hold 2 floats. Will this work in sail's? Else how can I get this to work. Thanks

PS: I'm working solely with MongoDB

5 Answers 5

6

As of Sails 1.0 type array is no longer supported.

The type "array" is no longer supported. To use this type in your model, change type to one of the supported types and set the columnType property to a column type supported by the model's adapter, e.g. { type: 'json', columnType: 'array' }

SOLUTION ONE

Set up property to store an images array and a location array...

module.exports = {
  attributes : {
    images: {
      type: 'json',                           
      columnType: 'array'
    }
    location: {
      type: 'json',
      columnType: 'array'
    }
  }
}

SOLUTION TWO

A more elegant solution is to set up a single object to store both filename and location data

module.exports = {
  attributes : {
    images: {
      type: 'json'
    }
  }
}

Then in your controller you would store object properties as arrays...

let imageData = {
  filename: ["image1.jpg", "image2.jpg", "image3.jpg"],
  location: [154.123123, 155.3434234, 35.12312312]
};

Images.create({images:imageData});

Some issues when storing data to the json object is that a string like "image1.jpg,image2.jpg,image3.jpg" will store in MongoDb no worries... doh. Ensure that when POSTing you may need to split the data .split(',').

Sign up to request clarification or add additional context in comments.

Comments

4

sailsjs provide a function to solve your question,you can try this

module.exports = {
 attributes : {
  images: {
   type: 'string'
  },
  location: {
   type: 'json'
  }
 }
}

3 Comments

Please add short explanation to your answer for future visitors.
I'm not sure how this is intended to work. The images attribute is supposed to be an array of strings but you have just string. Also from location's syntax, this specifies that it's value can either be float or float
sorry , im just edditing my post for solve your prob
2

As far as I know, you can only specify it like this:

module.exports = {
    attributes : {
        images: {
            type: 'array'
        },
        location: {
            type: 'array'
        }
    }
}

See Sails ORM Attributes

1 Comment

The type "array" is no longer supported. To use this type in your model, change type to one of the supported types and set the columnType property to a column type supported by the model's adapter, e.g. { type: 'json', columnType: 'array' }
1

For sails 1.0, for array maybe you can try this way i'm using just for sharing. Also you can replace before update and process native query() and delete the attributes for updating by waterline. Hope this help you.

        variants:
        {
            type: 'json',
            columnType: 'array',
            custom: (value) =>
            {
                /*
                  [
                        code    : unique, string
                        name    : string, maxLength[30]
                        cost    : number, isFinite
                        price   : number, isFinite
                        default : boolean
                  ]
                */
                return _.isArray(value)
                &&     _.every(value, (variant1) =>
                {
                    return _.countBy(value, (variant2) =>
                    {
                        return variant2.code == variant1.code ? 'eq' : 'uneq';
                    }).eq <= 1
                    && _.isString(variant1.name) && variant1.name.length < 30
                    && _.isNumber(variant1.cost) && _.isFinite(variant1.cost)
                    && _.isNumber(variant1.price) && _.isFinite(variant1.price)
                    && _.isBoolean(variant1.default);
                });
            },
        },

Comments

-1

You can use type as ref for arrays and objects.

module.exports = {
 attributes: {
  images: {
   type: 'ref'
  },
  location: {
   type: 'ref'
  }
 }
}

Comments

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.