单击功能不起作用并显示任何内容(mongo / node)

时间:2015-12-10 04:40:48

标签: jquery mongodb

所以基本上我只想在点击"所有吸血鬼"之后显示所有吸血鬼的列表。按钮。我想至少这个按钮现在可以工作了。但是,它没有工作,也没有显示任何内容,但我查看了我的代码,我接近确定(虽然我可能错了)我的代码没有任何问题。也许我在我的一个档案中遗漏了一些东西,我不知道。我需要一切,设置我的基本路线,模型和架构。有人可以指出问题是什么吗?请帮忙!

server.js文件:

var express = require('express');
var app = express();
var mongoose = require ('mongoose');
var bodyParser = require('body-parser');
var morgan = require('morgan');

var Vampire = require('./models/vampire.js');
var Ghoul = require('./models/ghoul.js');


mongoose.connect('mongodb://localhost/darkthings');
app.use(express.static('public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.listen(3000);

console.log('server working');

// vampires ---------------------
var vampire1 = new Vampire({
    name: "Vangeek",
    loves: ["women", "blahblah", "cheese"],
    victims: 75
});

var vampire2 = new Vampire({
    name: "Mindy",
    loves: ["blood", "dogs", "moarblood"],
    victims: 110
});

var ghoul1 = new Ghoul({
    ghoul_type: "Monster",
    haunts: ["forest", "wood", "kawasaki"],
    graves: 30
});

var ghoul2 = new Ghoul({
    ghoul_type: "Evil Spirit",
    haunts: ["babies", "troublemakers", "scallops"],
    graves: 55
});
// saving ------------------------------------
// vampire1.save(function(err) {
//   if (err) console.log(err);
//   console.log(vampire1.name + ' created!');
// });
//
// vampire2.save(function(err) {
//   if (err) console.log(err);
//   console.log(vampire1.name + ' created!');
// });

 // GET ----------------------------------------
 app.get('/vampires', function(req, res) {
    console.log("getting /vampires");
    Vampire.find().exec(function(err, vampires) {
    res.send(vampires);
    });
 });

 app.get('/ghouls', function(req, res) {
    console.log("getting /ghouls");
    Ghoul.find().exec(function(err, vampires) {
    res.send(ghouls);
   });
});


//SHOW ------------------------------------------
app.get('/vampires/:id', function(req, res) {
    console.log("getting /vampires/"+req.params.id);
    Vampire.findOne({ _id: req.params.id }, function(err, vampire) {
    });
});

app.get('/ghouls/:id', function(req, res) {
    console.log("getting /ghouls"+req.params.id);
    Ghouls.findOne({_id: req.params.id}, function(err, ghoul) {
    });
 });

vampire.js文件:

var mongoose = require('mongoose'); 

var vampireSchema = new mongoose.Schema({
    name: String,
    loves: Array,
    victims: Number
})

var Vampire = mongoose.model('Vampire', vampireSchema);
module.exports = Vampire;

html文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Vampiresss</title>
  </head>
  <body>

    <button id="all-vampires">All Vampires</button>
    <button id="vampire">One Vampire</button>
    <button id="all-ghouls">All Ghouls</button>
    <button id="ghoul">One Ghoul</button>



    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript" src="apps.js"></script>
  </body>
</html>

   apps.js file:
    $(document).ready(function() {

console.log("js working");

var $vampires = $('#all-vampires');
var $vampire = $('#vampire');
var $ghouls = $('#all-ghouls');
var $ghoul = $('#ghoul');
var $body = $('body');

// buttons ------------------------------------
$vampires.click(function() {
    console.log("button clicked");
    $a.jax({
        url: 'http://localhost:3000/vampires'
    }). done(function (data) {
        console.log("getting /vampires");
        $body.append("<h3>"+data+"</h3");
    });
});

});

这是我的populateVampires.js文件(我的所有数据):

{
  name: 'Count Chocula',
  hair_color: 'brown',
  eye_color: 'brown',
  dob: new Date(1971, 2, 13, 7, 47),
  loves: ['cereal','marshmallows'],
  location: 'Minneapolis, Minnesota, US',
  gender: 'm',
  victims: 2
},{
  name: 'Dracula',
  dob: new Date(937, 0, 24, 13, 0),
  hair_color: 'brown',
  eye_color: 'blue',
  loves: ['Winona Ryder', 'top hats', 'fancy cloaks', 'handlebar   mustaches'],
  location: 'Transylvania, Romania',
  gender: 'm',
  victims: 1238
},{
  name: 'Elizabeth Bathory ',
  dob: new Date(1560, 8, 7, 22, 10),
  hair_color: 'brown',
  eye_color: 'brown',
  loves: ['virgin blood', 'fancy cloaks','frilly collars'],
  location: 'Nyírbátor, Hungary',
  gender: 'f',
  victims: 980
},{
  name: 'Lestat',
  dob: new Date(1760, 11, 9, 18, 44),
  hair_color: 'blonde',
  eye_color: 'blue',
  loves: ['frilly shirtsleeves', 'frilly collars', 'lurking in   rotting mansions', 'Louis'],
  location: 'Auvergne, France',
  gender: 'm',
  victims: 324
},{
  name: 'Louis de Pointe du Lac',
  dob: new Date(1766, 6, 4, 2, 1),
  hair_color: 'brown',
  eye_color: 'blue',
  loves:['brooding', 'Claudia', 'staring longingly into the   distance'],
  location: 'New Orleans, Louisiana, US',
  gender:'m',
  victims: 150
},{
  name:'Claudia',
  dob: new Date(1793, 2, 7, 8, 30),
  hair_color: 'blonde',
  eye_color: 'blue',
  loves: ['doll babies', 'ribbons', 'appearing innocent', '  trickery'],
  location: 'New Orleans, Louisiana, US',
  gender: 'f',
  victims: 290
},{
  name:'Armand',
  dob: new Date(1481, 6, 1, 10, 42),
  hair_color: 'red',
  eye_color: 'brown',
  loves: ['theatre', 'painting', 'velvet robes', 'being tragic'],
  location: 'Kiev, Russia',
  gender: 'm',
  victims: 1045
},{
  name:'Santino',
  dob: new Date(1465, 6, 1, 10, 42),
  hair_color: 'brown',
  eye_color: 'brown',
  loves: ['trickery', 'vampiric cults', 'fancy cloaks'],
  location: 'Rome, Italy',
  gender: 'm',
  victims: 1103
},{
  name:'Akasha',
  dob: new Date(-8000, 6, 1, 10, 42),
  hair_color: 'brown',
  eye_color: 'green',
  loves: ['eating hearts', 'bathing in roses', 'elaborate   headdresses', 'R&B music'],
  location: 'Kemet, Egypt',
  gender: 'f',
  victims: 210234,
  title: 'Queen of the Damned'
}

0 个答案:

没有答案