JavaScript - 无法GET/api/list

T.J*_*T.J 3 javascript

我正在一个用户应该能够将项目发布到列表的网站上工作.现在,当我尝试发布某些内容时,会出现错误信息

Failed to load resource: the server responded with a status of 422 (Unprocessable Entity).
Run Code Online (Sandbox Code Playgroud)

在控制台中点击它时,它会打开一个新的水龙头

Cannot GET /api/list
Run Code Online (Sandbox Code Playgroud)

它也在命令提示符中说

Unhandled rejection Error: Can't set headers after they are sent.
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样,我能做些什么来解决它?以下是我的代码的一些片段:

index.html的:

fetch('/api/list', options)
    .then(response => response.json())
    .then(response => {
          if (response.status == 'OK') {
          console.log('song is added')
          getList(items)

          } else {
          alert(response.message)
        }
    })
 }
Run Code Online (Sandbox Code Playgroud)

Server.js:

app.post('/api/list', userIsAuthenticated, (req, res) => {
  let {
    titleArtist
    } = req.body

   let user_id = req.session.user.id

   // seaching for user id in database
   let query = {
     where: {
     userId: user_id
    }
  }
Run Code Online (Sandbox Code Playgroud)

它可能也是代码中的其他地方出错了.如果我应该发布更多代码片段,请告诉我.

Sun*_*arg 5

这是因为您正在向POST API发出GET请求.

这是你如何发出POST请求

fetch(url, {
  method: 'POST', // or 'PUT'
  body: JSON.stringify(data), // data can be `string` or {object}!
  headers:{
    'Content-Type': 'application/json'
  }
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
Run Code Online (Sandbox Code Playgroud)