2016-04-04 7 views
0

Haben einige Probleme mit einer Knoten-App, die ich baue, die Bearbeitungsschaltfläche, die ich an die Seite angefügt hat, funktioniert nicht (nicht den spezifischen Eintrag) ich bekomme nur eine 404 Fehler, wenn ich getroffen bearbeitenBearbeiten in Node.js App funktioniert nicht

Geschäfte/index.ejs

<table class="table table-striped"> 
     <thead> 
     <tr> 
      <th>Pet Store Name</th> 
      <th>Address</th> 
      <th>Phone Number</th> 
      <th>Website</th> 
     </tr> 
     </thead> 
     <tbody> 
     <!--starting table loop--> 
     <% for (var i = 0; i < stores.length; i++) { %> 
     <tr> 
      <td><%= stores[i].name %></td> 
      <td><%= stores[i].address %></td> 
      <td><%= stores[i].phone %></td> 
      <td><a href="http://<%= stores[i].website %>">http://<%= stores[i].website %></a></td> 
      <td><a href="/stores/edit/<%= stores[i]._id %>"><button class="btn btn-primary" 
                  type="button">Edit</button></a></td> 
     </tr> 
     <% } %> 
     </tbody> 
    </table> 

edit.ejs

<form method="post" action="<%= stores._id %>"> 
     <fieldset class="form-group"> 
      <label for="name" class="col-sm-2">Pet Store Name: </label> 
      <input name="name" id="name" required value="<%= stores.name %>" /> 
     </fieldset> 
     <fieldset class="form-group"> 
      <label for="address" class="col-sm-2">Address: *</label> 
      <input name="address" id="address" required value="<%= stores.address %>" /> 
     </fieldset> 
     <fieldset class="form-group"> 
      <label for="phone" class="col-sm-2">Phone Number: *</label> 
      <input name="phone" id="phone" value="<%= stores.phone %>" /> 
     </fieldset> 
     <fieldset class="form-group"> 
      <label for="website" class="col-sm-2">Website: </label> 
      <input name="website" id="website" value="<%= stores.website %>" /> 
     </fieldset> 
     <div> 
      <%= stores.created %> 
     </div> 
     <button class="btn btn-primary col-sm-offset-2">Save</button> 

     <a href="/stores/delete/<%= stores._id %>" title="Delete" class="confirmation"> 
      <button class="btn btn-danger" type="button">Delete</button> 
     </a> 
    </form> 

index.js

// link to dependencies 
var express = require('express'); 
var router = express.Router(); 
var mongoose = require('mongoose'); 
var passport = require('passport'); 

//link to store model 
var Store = require('../models/store'); 

//set up get handler for password protected store page 
router.get('/', isLoggedIn, function (req, res, next) { 
    //use the store model to get directory listing 
    Store.find(function (err, stores) { 
     if (err) { 
      console.log(err); 
      res.end(err); 
     } 
     else { 
      //show we got data back, show the view and pass the data through it 
      res.render('stores/index', { 
       title: 'Pet Store Directory', 
       stores: stores 
      }); 
     } 
    }) 
}); 

// get handler for add to display a blank form 
router.get('/add', isLoggedIn, function (req, res, next) { 
    res.render('stores/add', { 
     title: 'Add a new Pet Store' 
    }); 
}); 

// post handler for adding to the form 
router.post('/add', isLoggedIn, function (req, res, next) { 

    //save new pet store 
    Store.create({ 
      name: req.body.name, 
      address: req.body.address, 
      phone: req.body.phone, 
      website: req.body.website 
     } 
    ); 

    // redirect to auth store page 
    res.redirect('/stores'); 
}); 

//get handler for edit to show content from entry 
router.get('/id', isLoggedIn, function(req, res, next){ 
    //create id variable to the store id from the url 
    var id = req.params.id; 

    //look up the selected store 
    Store.findById(id, function(err, store){ 
     if (err) { 
      console.log(err); 
      res.end(err); 
     } 
     else { 
      //show edit form 
      res.render('stores/edit', { 
       title: 'Store Details', 
       store: store 
      }); 
     } 
    }); 
}); 

//post handler for edit to update the store 
router.post('/id', isLoggedIn, function(req, res, next){ 
    //create id variable to the store id from the url 
    var id = req.params.id; 

    //fill the store object 
    var store = new Store({ 
     name: req.body.name, 
     address: req.body.address, 
     phone: req.body.phone, 
     website: req.body.website 
    }); 

    //use mongoose and store model to update 
    Store.update({_id: id}, store, function(err){ 
     if (err){ 
      console.log(err); 
      res.end(err); 
     } 
     else { 
      res.redirect('/stores') 
     } 
    }); 
}); 

//get handler for delete using the store id 
router.get('/delete/:id', isLoggedIn, function(req, res, next){ 
    //get id variable to the store id from the url 
    var id = req.params.id; 

    console.log('trying to delete'); 

    Store.remove({_id: id}, function(err){ 
     if (err) { 
      console.log(err); 
      res.end(err); 
     } 
     else { 
      // show updated article list 
      res.redirect('/stores'); 
     } 
    }); 
}); 

//authorization check 
function isLoggedIn(req, res, next) { 

    //is the user authenticated 
    if (req.isAuthenticated()){ 
     return next(); 
    } 
    else { 
     res.redirect('/auth/login'); 
    } 
} 

// make public 
module.exports = router; 

Ich habe den Code für Stunden übergossen, es würde helfen, eine neue Reihe von Augen darauf zu bekommen.

Github ist github.com/krl87/comp2106a2

bei comp2106a2.herokuapp.com gehostet

Dank im Voraus! kayley

Antwort

0

Ich sehe keine Route für /edit/:id überall. Es gibt eine, die nur /id sagt. Ich denke, du wolltest dort /edit/:id setzen. So ändern:

router.get('/id', isLoggedIn, function(req, res, next){ 

zu

router.get('/edit/:id', isLoggedIn, function(req, res, next){ 

Gleiche mit zu schreiben. Wechsel:

router.post('/id', isLoggedIn, function(req, res, next){ 

zu

router.post('/edit/:id', isLoggedIn, function(req, res, next){ 
+0

ich geändert, dass (danke für das Fang!), Aber ich bin immer noch die gleichen Fehler. [img] http://i.imgur.com/2ljnsa3.png [/ img] Ich gehe davon aus, dass es irgendwo einen Pfadfehler gibt? – KRL87