【问题标题】:I cant delete data from my Mean stack app我无法从平均堆栈应用程序中删除数据
【发布时间】:2017-11-10 21:23:12
【问题描述】:

我的创建、读取、更新操作都在工作,除了删除操作和使用邮递员它给了我同样的错误。我正在使用 mlab 来存储我的数据

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Student = require('../models/student');


const db = 'mongodb://<dbuser>:<dbpassword>@ds036577.mlab.com:36577/andela';
mongoose.Promise = global.Promise;
mongoose.connect(db, function (err) {
  if (err) {
    console.log('Error' + err);
  }
});

router.delete('student/:id', function (req, res) {
  console.log('Deleting a student');
  Student.findByIdAndRemove(req.params.id, function (err, deletedStudent) {
    if (err) {
      res.send('Error Deleting Data');
    } else {
      res.json(deletedStudent);
    }
  });
});

module.exports = router;

邮递员的错误

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot DELETE /api/students/59fcc17a8a076e216057e116</pre>
    </body>
</html>

来自控制台的错误

core.es5.js:1020 错误 回复 标题 : 标题 {_headers: Map(7), _normalizedNames: Map(7)} 好的 : 错误的 地位 : 404 状态文本 : “未找到” 类型 : 2 网址 : "http://localhost:3000/api/student/5a04b50e2732c630205aec09" _身体 : “↵↵↵↵错误↵↵↵

无法删除 /api/student/5a04b50e2732c630205aec09
↵↵↵" 原型 : 身体

其他操作的代码类似,我不明白为什么它不起作用。我已经尝试调试好几天了。

router.get('/students', function (req, res) {
  console.log('Request for all students');
  Student.find({}).exec(function (err, students) {
    if (err) {
      console.log('Error getting Data')
    } else{
      res.json(students);
    }
  });
});

router.get('/students/:id', function (req, res) {
  console.log('Request for a student');
  Student.findById(req.params.id).exec(function (err, student) {
    if (err) {
      console.log('Error getting Data')
    } else{
      res.json(student);
    }
  });
});

router.post('/student', function (req, res) {
  console.log('add a new student');
  var newStudent = new Student();
  newStudent.FullName = req.body.FullName;
  newStudent.ImageUrl = req.body.ImageUrl;
  newStudent.CourseOfStudy = req.body.CourseOfStudy;
  newStudent.YearOfEntry = req.body.YearOfEntry;
  newStudent.DurationOfStudy = req.body.DurationOfStudy;
  newStudent.CurrentLevel = req.body.CurrentLevel;

  // newStudent. expectedYearOfGraduation = req.body.expectedYearOfGraduation;
  newStudent.save(function (err, insertedStudent) {
    if (err) {
      console.log('Error saving student');
    } else {
      res.json(insertedStudent);
    }
  })
});


router.put('/student/:id', function (req, res) {
  console.log('Update the data');
  Student.findByIdAndUpdate(req.params.id,
    {
      $set: {
        FullName : req.body.FullName,
        ImageUrl : req.body.ImageUrl,
        CourseOfStudy : req.body.CourseOfStudy,
        YearOfEntry : req.body.YearOfEntry,
        DurationOfStudy : req.body.DurationOfStudy,
        CurrentLevel : req.body.CurrentLevel}
    },
  {
      new: true
  },
  function (err, updatedStudent) {
    if(err) {
      res.send('Error Updating Video');
    } else {
      res.json(updatedStudent);
    }
  }
  )

});

【问题讨论】:

  • 错误是什么?
  • 错误无法删除 /api/students/59fcc17a8a076e216057e116
  • 不,我的意思是,您的代码中有if (err) {。如果你console.log(err) 会发生什么?您向我展示的错误只是 API 错误,而不是 mongo 错误。另外,请在问题本身中发布错误,而不是在 cmets 中。
  • 你能显示其他路线的代码吗?很难说有什么区别。
  • 我注意到的一件事是你的邮递员错误有students(复数),而你在代码中的路线有student(单数)。

标签: node.js express mlab


【解决方案1】:

错误来自我没有在学生前面放“/”的路线

router.delete('/student/:id', function (req, res) {
  console.log('Deleting a student');
  Student.findByIdAndRemove(req.params.id, function (err, deletedStudent) {
    if (err) {
      res.send('Error Deleting Data');
    } else {
      res.json(deletedStudent);
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2018-07-13
    • 2017-11-28
    相关资源
    最近更新 更多