Parcourir la source

debugged with database.

Tomi Cvetic il y a 8 ans
Parent
commit
ab284e58ed
1 fichiers modifiés avec 25 ajouts et 22 suppressions
  1. 25 22
      project/route.js

+ 25 - 22
project/route.js

@@ -4,45 +4,47 @@ import express from 'express'
 
 export function getHandler (req, res) {
   /** GET handler (request one or more projects) */
-  let [ id ] = req.params
+  let id = req.params['0']
 
   /** check whether an id was specified. */
   if (typeof id !== 'undefined') {
     try {
-      id = mongoose.Types.ObjectId(id.substring(1))
+      id = mongoose.Types.ObjectId(id)
     } catch (err) {
       res.status(422)
       res.send(err)
       return
     }
     /** if yes, return the one project */
-    Project.findOne({ _id: id }, function (err, project) {
-      if (err) {
-        res.status(404)
-        res.send(err)
-        return
-      }
-      res.json(project)
-    })
+    Project.findOne({ _id: id })
+      .exec((err, project) => {
+        if (err) {
+          res.status(404)
+          res.send(err)
+          return
+        }
+        res.json(project)
+      })
   } else {
     /** if not, return all projects */
     const { limit, offset } = req.query
-    Project.find({}, {
-      limit: Math.min(Math.max(parseInt(limit) || 20, 1), 100),
-      skip: Math.max(parseInt(offset) || 0, 0)
-    }, function (err, projects) {
-      if (err) {
-        res.status(404)
-        res.send(err)
-        return
-      }
-      res.json(projects)
-    })
+    Project.find()
+      .limit(Math.min(Math.max(parseInt(limit, 10) || 20, 1), 100))
+      .skip(Math.max(parseInt(offset, 10) || 0, 0))
+      .exec((err, projects) => {
+        if (err) {
+          res.status(404)
+          res.send(err)
+          return
+        }
+        res.json(projects)
+      })
   }
 }
 
 export function postHandler (req, res) {
   /** POST handler (insert new projects into database) */
+  console.log(req.body)
   const project = new Project(req.body)
 
   project.save(function (err) {
@@ -57,7 +59,7 @@ export function postHandler (req, res) {
 
 export function putHandler (req, res) {
   /** PUT handler (update existing project) */
-  const id = mongoose.Types.ObjectId(req.params['0'].substring(1))
+  const id = mongoose.Types.ObjectId(req.params['0'])
 
   Project.findOne({ _id: id }, function (err, project) {
     if (err) {
@@ -65,6 +67,7 @@ export function putHandler (req, res) {
       res.send(err)
     }
 
+    console.log(project, req.body)
     for (let prop in req.body) {
       project[prop] = req.body[prop]
     }