Search code examples
node.jspostgresqlback

An object in array, what dataType?


I want put front to back objet in an array. But i do not see how to do this with dataType. I want this :

"task": [
    {
      "car": "feri",
      "color": "blue"
    }
  ],
  "id_user": 1

In my data table i make this :

  "id" serial PRIMARY KEY,
  "task" text[],

And in the model this :

 task: DataTypes.ARRAY(DataTypes.TEXT),
    id_user : DataTypes.INTEGER

So when i put in back, in the dataTable i have this :

"id": 1,
      "task": [
        "{\"car\":\"feri\",\"color\":\"blue\"}"
      ],

Now i see my error, i make text in an array. But i want objet and i do not see how.

How can i make that ?

Thanks for read :)


Solution

  • If you wish to store an array of objects then just use JSON or JSONB data type in a DB and task: DataTypes.JSON in a model.