Sequelize - .update - setTags 不是函数
Sequelize - .update - setTags is not a function
“setTags 不是函数”
我有 Post 和标签的多对多关系。
db.tag.belongsToMany(db.post, {
through: 'post_tags',
foreignKey: 'tagId',
otherKey: 'postId',
})
db.post.belongsToMany(db.tag, {
through: 'post_tags',
foreignKey: 'postId',
otherKey: 'tagId',
})
我正在尝试更新 Post 的内容,包括相关标签。
从第 7 行开始:
- 检查标签
- 匹配找到的标签
- 设置post
的标签
exports.update = (req, res) => {
const id = req.params.id
Post.update(req.body, {
where: { id: id },
})
.then((number) => {
if (req.body.tags) {
Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
},
}).then((tag_items) => {
res.setTags(tag_items)
})
}
if (number == 1) {
res.send({
message: 'This post attempt was successful.',
})
} else {
res.send({
message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
})
}
})
.catch((err) => {
res.status(500).send({
message: 'There was an error updating post id=' + id,
})
})
}
我使用非常相似的东西来创建 Post。
我希望它能像它那样工作。
看了这么多文档和网上搜索。
在这一点上,我觉得它一定是我忽略的简单东西。
甚至可能是拼写错误或某物的错误定位。
我尝试使用 findByPk 创建响应数据作为 Post。
然后 运行 使用 setTags,我仍然得到同样的错误。
不确定是否可能因为 Post.update returns 不同的数据?
比 Post.create,我想我读了一些关于这个的东西。
但我曾尝试添加另一个参数,但没有得到预期的结果。
如果您能提供任何建议,我们将不胜感激。
谢谢!
您在 res
中调用了 setTags
,这不是 Post
的 Sequeize 模型。
您需要先像这样获取 Post
实例:
Post.findOne({
where: { id: id },
}).then((post) => {
if (req.body.tags) {
Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
},
}).then((tag_items) => {
post.setTags(tag_items)
})
}
})
在这种情况下,我建议使用 async/await
来获得 straint-forward 代码而不是 then
链:
exports.update = async (req, res) => {
const id = req.params.id
try {
const number = await Post.update(req.body, {
where: { id: id },
})
if (req.body.tags) {
const post = await Post.findOne({
where: { id: id },
})
const tag_items = await Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
});
post.setTags(tag_items)
}
if (number == 1) {
res.send({
message: 'This post attempt was successful.',
})
} else {
res.send({
message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
})
}
} catch((err) {
res.status(500).send({
message: 'There was an error updating post id=' + id,
})
}
}
“setTags 不是函数”
我有 Post 和标签的多对多关系。
db.tag.belongsToMany(db.post, {
through: 'post_tags',
foreignKey: 'tagId',
otherKey: 'postId',
})
db.post.belongsToMany(db.tag, {
through: 'post_tags',
foreignKey: 'postId',
otherKey: 'tagId',
})
我正在尝试更新 Post 的内容,包括相关标签。
从第 7 行开始:
- 检查标签
- 匹配找到的标签
- 设置post 的标签
exports.update = (req, res) => {
const id = req.params.id
Post.update(req.body, {
where: { id: id },
})
.then((number) => {
if (req.body.tags) {
Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
},
}).then((tag_items) => {
res.setTags(tag_items)
})
}
if (number == 1) {
res.send({
message: 'This post attempt was successful.',
})
} else {
res.send({
message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
})
}
})
.catch((err) => {
res.status(500).send({
message: 'There was an error updating post id=' + id,
})
})
}
我使用非常相似的东西来创建 Post。
我希望它能像它那样工作。
看了这么多文档和网上搜索。
在这一点上,我觉得它一定是我忽略的简单东西。
甚至可能是拼写错误或某物的错误定位。
我尝试使用 findByPk 创建响应数据作为 Post。
然后 运行 使用 setTags,我仍然得到同样的错误。
不确定是否可能因为 Post.update returns 不同的数据?
比 Post.create,我想我读了一些关于这个的东西。
但我曾尝试添加另一个参数,但没有得到预期的结果。
如果您能提供任何建议,我们将不胜感激。
谢谢!
您在 res
中调用了 setTags
,这不是 Post
的 Sequeize 模型。
您需要先像这样获取 Post
实例:
Post.findOne({
where: { id: id },
}).then((post) => {
if (req.body.tags) {
Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
},
}).then((tag_items) => {
post.setTags(tag_items)
})
}
})
在这种情况下,我建议使用 async/await
来获得 straint-forward 代码而不是 then
链:
exports.update = async (req, res) => {
const id = req.params.id
try {
const number = await Post.update(req.body, {
where: { id: id },
})
if (req.body.tags) {
const post = await Post.findOne({
where: { id: id },
})
const tag_items = await Tag.findAll({
where: {
name: {
[Op.or]: req.body.tags,
},
});
post.setTags(tag_items)
}
if (number == 1) {
res.send({
message: 'This post attempt was successful.',
})
} else {
res.send({
message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
})
}
} catch((err) {
res.status(500).send({
message: 'There was an error updating post id=' + id,
})
}
}