gpt4 book ai didi

node.js - Sequelize-Postgres错误: values. map is not a function

转载 作者:搜寻专家 更新时间:2023-10-31 23:23:51 32 4
gpt4 key购买 nike

我正在开发 Node + Postgres 应用程序,但在使用 Sequelize 模型架构设置 DataType.ARRAY 时遇到了问题。

这是我的模型架构,

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);
}
}
});

我使用 Postman 来测试 API,下面是我如何将数据 POST 到服务器, enter image description here这是 Node 控制台上的错误跟踪。

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

可能是什么原因?请帮忙。

最佳答案

对于application/x-www-form-urlencoded,发送到服务器的 HTTP 消息主体本质上是一个巨大的查询字符串。

因此,tags 的值在后端接收时将是字符串格式。在后端将它转换回 Array 类型,你应该可以开始了

关于node.js - Sequelize-Postgres错误: values. map is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827651/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com