I'm working on Node + Postgres app and I have trouble in setting the DataType.ARRAY using Sequelize model schema.
Here is my model schema,
var ArtistRoles = sequelize.define('ArtistRoles', {
id:{
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1
},
name:{
type: DataTypes.STRING,
allowNull: false
},
tags: {
type: DataTypes.ARRAY(DataTypes.STRING),
defaultValue: []
}
},{
hooks:{
beforeCreate: function (role, options) {
//role.tags = JSON.stringify(role.tags);
console.log(role.tags);
}
},
classMethods: {
associate: function (models) {
// add relations model relations here
//ArtistRoles.belongsTo(models.Artists);
}
}
});
I used Postman to test the API and here is how I POST the data to server, Here is the Error trace on Node console.
TypeError: values.map is not a function
at ARRAY.BaseTypes.ARRAY.$stringify (~/PROJECTS/my-api/node_modules/sequelize/lib/dialects/postgres/data-types.js:385:33)
at ARRAY.ABSTRACT.stringify (~/PROJECTS/my-api/node_modules/sequelize/lib/data-types.js:77:17)
at Object.escape (~/PROJECTS/my-api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:967:32)
at Object.insertQuery (~/PROJECTS/my-api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:299:28)
at QueryInterface.insert (~/PROJECTS/my-api/node_modules/sequelize/lib/query-interface.js:497:33)
at .<anonymous> (~/PROJECTS/my-api/node_modules/sequelize/lib/instance.js:672:56)
at tryCatcher (~/PROJECTS/my-api/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (~/PROJECTS/my-api/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (~/PROJECTS/my-api/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (~/PROJECTS/my-api/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (~/PROJECTS/my-api/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (~/PROJECTS/my-api/node_modules/bluebird/js/release/async.js:138:16)
at Async._drainQueues (~/PROJECTS/my-api/node_modules/bluebird/js/release/async.js:148:10)
at Immediate.Async.drainQueues (~/PROJECTS/my-api/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:637:20)
at tryOnImmediate (timers.js:610:5)
POST /api/1.0/role 500 46.114 ms - 45
What could be the reason? Please help.
For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string.
So, the value of tags will be in string format when received in backend. Convert it back into Array type in backend and you should be good to go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With