简体   繁体   中英

NodeJS + Express + Mongoose(MongoDB) Database Insert Error

I'm getting the error "Sun Jun 12 15:27:12 SyntaxError: missing; before statement (shell):1" in the mongodb logs when I execute the following code using NodeJS/Express/Mongoose. I do not get an error returned from the function. Any guidance would be much appreciated.

// Launch express and server
var express = require('express');
var app = express.createServer();

//connect to DB
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://127.0.0.1/napkin_0.1');

// Define Model
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    name : String,
    age : String
});

mongoose.model('Document', UserSchema);
var User = mongoose.model('Document');

var user = new User();

user.name = 'Jim';
user.age = '27';
user.save(function(err, user_Saved){
    if(err){
        throw err;
        console.log(err);
    }else{
        console.log('saved!');
    }
});

//Launch Server
app.listen(3002);

DB name should not contain a '.'. Just remove the. and it will work fine.

@matty shouldn t we remove the "throw err;"

because there is 2 callback on the error if we kke it there

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