简体   繁体   English

排序,使用React删除多行

[英]Sequelize, Deleting multiple rows with React

I'm using React with a Postgres DB with Sequelize. 我正在将React与带有Sequelize的Postgres数据库一起使用。 within my project, I have a promise that is "suppose" to delete all songs relating to the album, using the the Album.id in my state. 在我的项目中,我承诺“应该”使用我所在状态的Album.id删除与专辑有关的所有歌曲。

** Instead of deleting the rows of songs relating to the Album, after the delete request in the database, it removes the value of the AlbumId of the song. **在数据库中执行删除请求之后,它不会删除与专辑相关的歌曲行,而是会删除歌曲的AlbumId值。 ** **

Is there an update I am missing 我是否缺少更新

When I console.log outside of the service and in the promise this.state.Album.id remains the same. 当我在服务之外并承诺使用console.log时,this.state.Album.id保持不变。 It hit's the server with the appropriate number. 它用适当的编号命中了服务器。

This is the function within the React Component DeleteAlbum (e) { e.preventDefault() 这是React组件DeleteAlbum(e){e.preventDefault()中的函数

    axios.delete(`${domain}/albums/${this.state.Album.id}`)
    .then((res) => {
      axios.delete(`${domain}/songs/ByAlbumId/${this.state.Album.id}`)
      .then((res) => {
        window.location.href = '/#/'
      })
      .catch((error) => {
        console.log('axios error', error)
      })
    })
    .catch((error) => {
      console.log('axios error', error)
    })
  }

This is the Database to the Songs Route 这是通往歌曲之路的数据库

const express = require('express')
const router = express.Router()
const bodyParser = require('body-parser')
const db = require('./../models')
const Song = db.Song

router.use(bodyParser.json({ extended: false }))

const exists = (req) => {
  if (typeof parseInt(req.params.id) === 'number') {
    Album.findOne({
      where: {
        id: req.params.id
      }
    })
    .then((album) => {
      if (album) {
        return true
      };
      return false
    })
    .catch((err) => {
      return false
    })
  } else {
    return false
  }
}

router.delete('/ByAlbumId/:id', function (req, res) {
  Song.destroy({
    where: {
      AlbumId: req.params.id
    }
  })
  .then(function (data) {
    return res.json(data)
  })
  .catch(function (err) {
    return res.json({ error: err})
  })
})

router.delete('/:id', function (req, res) {
  if (exists) {
    Song.destroy({
      where: {
        id: req.params.id
      }
    })
    .then(function (data) {
      return res.json(data)
    })
    .catch(function (err) {
      return res.json({ error: err})
    })
  } else {
    res.json({success: false})
  }
})

This is the Album Route 这是专辑路线

const express = require('express')
const router = express.Router()
const bodyParser = require('body-parser')
const db = require('./../models')
const Album = db.Album

router.use(bodyParser.json({ extended: false }))

const exists = (req) => {
  if (typeof parseInt(req.params.id) === 'number') {
    Album.findOne({
      where: {
        id: req.params.id
      }
    })
    .then((album) => {
      if (album) {
        return true
      };
      return false
    })
    .catch((err) => {
      return false
    })
  } else {
    return false
  }
}

router.delete('/:id', function (req, res) {
  if (exists) {
    Album.destroy({
      where: {
        id: req.params.id
      }
    })
    .then(function (data) {
      return res.json(data)
    })
    .catch(function (err) {
      return res.json({ error: err})
    })
  } else {
    res.json({success: false})
  }
})

If I place console logs all over the place, the output is what I expect it to be. 如果我到处都放置控制台日志,那么输出就是我期望的那样。 There's is just something going wrong with Deleting two songs from my app. 从我的应用中删除两首歌曲只是出了点问题。 I can delete multiple songs if I hit the server directly with postman 如果我直接用邮递员访问服务器,则可以删除多首歌曲

Any idea? 任何想法?

You are actually destroying the album, before you destroy the songs. 在销毁歌曲之前,实际上是销毁专辑。

In this case, since they probably have onDelete: 'SET NULL' option added, you will just de-associate the songs with that album. 在这种情况下,由于它们可能已添加onDelete: 'SET NULL'选项,因此您只需将歌曲与该专辑onDelete: 'SET NULL'关联。

Your fix will be to just replace the order of your calls : 您的解决方法是只替换呼叫顺序:

 // First we delete the songs and then the album
 axios.delete(`${domain}/songs/ByAlbumId/${this.state.Album.id}`)
     .then((res) => {
         axios.delete(`${domain}/albums/${this.state.Album.id}`)
            .then((res) => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM