传播运算符在 javascript 中无法正常工作
Spread operator not working properly in javascript
我在用户和 post 之间有一个 hasMany 关系。在下面的代码中,如果 post 尚不存在,我将尝试将 posts 添加到用户模型的 userposts 字段。如果 post 已经存在,我基本上想用所有旧的 posts 和新的 posts 更新用户 posts 字段。 userposts 是存在于用户模型中的 JSON 字段。 posted 保存用户模型中存在的用户posts 字段的值。
当我添加一个新的 post 时,我可以看到它被添加到用户 post 字段中,但是当我尝试添加更多 post 时,在数据库中我得到用户posts数据如下:["Adding posts 2", "A", "d", "d", "i", "n", "g", " " ,“p”,“o”,“s”,“t”,“s”]。我需要它是这样的 ["Adding posts 2", "Adding posts"]
为什么会这样?我该如何解决这个问题?
app.post('/topic', async (req,res) =>{
const availPosts = await Post.create({
text: req.body.postText,
UserId: req.body.UserId
})
const findUser = await User.findOne({
where: {id:req.body.UserId}
})
// Code runs when posts already exists
const posted = findUser.userposts // holds the value of a user's post
if(posted){
const newPost = ([req.body.postText, ...posted])
let updatedPosts = await User.update({userposts:newPost },
{ where: {id: req.body.UserId}},
)
}
else {
let updatedPosts = await User.update({userposts:req.body.postText },
{ where: {id: req.body.UserId}},
)
}
res.send('working')
})
添加第一个 post 时,您将其设置为单个字符串而不是字符串数组。因此,当您下次搜索用户时,findUser.userposts
是一个字符串而不是一个数组,展开运算符执行它定义的操作并将字符串拆分为一个字符数组。
第一个 post 上的 userposts
的值不是仅使用 req.body.postText
,而是使用包含该字符串的数组,如下所示:
if(posted){
const newPost = ([req.body.postText, ...posted])
let updatedPosts = await User.update({userposts:newPost },
{ where: {id: req.body.UserId} }
)
}
else {
let updatedPosts = await User.update({userposts: [req.body.postText] }, //use array here, instead of string
{ where: {id: req.body.UserId}}
)
}
我在用户和 post 之间有一个 hasMany 关系。在下面的代码中,如果 post 尚不存在,我将尝试将 posts 添加到用户模型的 userposts 字段。如果 post 已经存在,我基本上想用所有旧的 posts 和新的 posts 更新用户 posts 字段。 userposts 是存在于用户模型中的 JSON 字段。 posted 保存用户模型中存在的用户posts 字段的值。
当我添加一个新的 post 时,我可以看到它被添加到用户 post 字段中,但是当我尝试添加更多 post 时,在数据库中我得到用户posts数据如下:["Adding posts 2", "A", "d", "d", "i", "n", "g", " " ,“p”,“o”,“s”,“t”,“s”]。我需要它是这样的 ["Adding posts 2", "Adding posts"]
为什么会这样?我该如何解决这个问题?
app.post('/topic', async (req,res) =>{
const availPosts = await Post.create({
text: req.body.postText,
UserId: req.body.UserId
})
const findUser = await User.findOne({
where: {id:req.body.UserId}
})
// Code runs when posts already exists
const posted = findUser.userposts // holds the value of a user's post
if(posted){
const newPost = ([req.body.postText, ...posted])
let updatedPosts = await User.update({userposts:newPost },
{ where: {id: req.body.UserId}},
)
}
else {
let updatedPosts = await User.update({userposts:req.body.postText },
{ where: {id: req.body.UserId}},
)
}
res.send('working')
})
添加第一个 post 时,您将其设置为单个字符串而不是字符串数组。因此,当您下次搜索用户时,findUser.userposts
是一个字符串而不是一个数组,展开运算符执行它定义的操作并将字符串拆分为一个字符数组。
第一个 post 上的 userposts
的值不是仅使用 req.body.postText
,而是使用包含该字符串的数组,如下所示:
if(posted){
const newPost = ([req.body.postText, ...posted])
let updatedPosts = await User.update({userposts:newPost },
{ where: {id: req.body.UserId} }
)
}
else {
let updatedPosts = await User.update({userposts: [req.body.postText] }, //use array here, instead of string
{ where: {id: req.body.UserId}}
)
}