简体   繁体   中英

When I run the node.js server, nothing happens

Is there anyone who's familiar with Node.js and mongoDB? Whenever I run the server, nothing happens. I'm supposed to see that console.log("WE JUST SAVED A CAT TO THE DB")

Here's the code of my app.js:

var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/cat_app", {
  useMongoClient: true
});
var catSchema = new mongoose.Schema({
  name: String,
  age: Number,
  temperament: String
});
var Cat = mongoose.model("Cat", catSchema);
// Add a new cat to the DB
var george = new Cat({
  name: "George",
  age: 11,
  temperament: "Grouchy"
});
george.save(function(err, cat) {
  if (err) {
    console.log("SOMETHING WENT WRONG!")
  } else {
    console.log("WE JUST SAVED A CAT TO THE DB!")
    console.log(cat);
  }
});
// Retrieve all cats from the DB and console.log each one

So is there any mistake up there?

This should help.

you start mongod //mongo server either though bash or file.

then start mongo // connect to your server

then node cats.js // in my case it's southpark.js

with code inside like this// and that should work.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/SP_app');


var spSchema = new mongoose.Schema({ 
    name: String,
    age: Number,
    temperament:String 
}); 


var SP = mongoose.model('SP', spSchema);

var eric = new SP({ 
    name:'Butters',
    age:10,
    temperament:'confused' 
});

eric.save(function(err, sp){ 
    if(err){  
        console.log("404 Something's Wrong") 
    }   else{ 
        console.log('WE JUST SAVED A CHARACTER TO THE DB:') 
        console.log(sp);  
    }
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM