javascript – NodeJS Express Mongoose(MongoDB)数据库插入错误

当我使用NodeJS / Express / Mongoose执行以下代码时,mongodb日志中出现错误“Sun Jun 12 15:27:12 SyntaxError:missing; before statement(shell):1”.我没有从函数返回错误.任何指导都将非常感谢.

// 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);

解决方法:

数据库名称不应包含’.’ .只需删除.它会工作正常.

上一篇:javascript – Async – parallel()与函数动态数组


下一篇:让 Mongoose 不再重复链接数据库(如何正确连接以解决升级后的报错)