POST端点不使用Express

时间:2018-04-27 17:23:20

标签: node.js reactjs express

我正在使用PERN堆栈编写每日日托报告应用程序,我遇到了一些POST端点问题。下面的一个正常工作:

> dat <- read.csv(text= "hd1 , hd2, hd3\n 1, a ,   c\n1,b,d\n")
> dat
  hd1 hd2  hd3
1   1  a     c
2   1   b    d
> dput(dat)
structure(list(hd1 = c(1L, 1L), hd2 = structure(1:2, .Label = c(" a ", 
"b"), class = "factor"), hd3 = structure(1:2, .Label = c("   c", 
"d"), class = "factor")), .Names = c("hd1", "hd2", "hd3"), class = "data.frame", row.names = c(NA, 
-2L))
> dat <- data.frame(
             lapply(read.csv(text= "hd1 , hd2, hd3\n 1, a ,   c\n1,b,d\n"), 
                    trimws)
                    )
# could also have used a two step process starting with the original `dat` 
# dat[] <- lapply(dat, trimws)   .... the `[]` preserves structure

> dat
  hd1 hd2 hd3
1   1   a   c
2   1   b   d
> dput(dat)
structure(list(hd1 = structure(c(1L, 1L), .Label = "1", class = "factor"), 
    hd2 = structure(1:2, .Label = c("a", "b"), class = "factor"), 
    hd3 = structure(1:2, .Label = c("c", "d"), class = "factor")), .Names = c("hd1", 
"hd2", "hd3"), row.names = c(NA, -2L), class = "data.frame")

但是我还有一些其他人用与错误相同的方式编写:

  

TypeError students.createStudent不是函数

下面的一个不起作用并抛出上述错误消息

  app.get('/classroomList', (req, res) => {
        classroom.getClassrooms().then((classrooms) => { res.send(classrooms) })
  })
  app.get('/classroomList/:id',(req,res)=>{
        classroom.getStudentsByClass(req.params.id).then((student)=>{
              res.send(student)
        })
  })

  app.post('/classroomList', (req, res) => {      
        classroom.createClassroom(req.body)
        .then((classroom)=>{res.send(classroom.attributes)})

              })
  app.delete('/classroomList/:id', (req, res) => {
        console.log('req received')
        classroom.deleteClassroom(req.params.id).then((classroom) => {
              res.send('Deleted')
        })
  })

} 

这是可行的控制器:

module.exports = (app) => {
  const students = require('../controllers/studentController')

  app.get('/studentList', (req, res) => {
        students.getStudents().then((students) => { res.send(students) })
  })

  app.delete('/studentList/:id', (req, res) => {
        students.deleteStudent(req.params.id).then((student) => {
              res.send('Deleted')
        })
  })
  app.post('/studentList', (req, res) => {  
        console.log(req.body)    
        students.createStudent(req.body)
        .then((student)=>{console.log(student.attributes)})

  })
} 

这个不是:

exports.createClassroom = (classroom) => {
  console.log('function accessed')
  const newClassroom = new Classroom(
        classroom)
  return newClassroom.save()
        .then(classroom => {
              return classroom;
        })
        .catch(err => {
              console.log(err)
        })

0 个答案:

没有答案