NodeJS +快递 + Mongoose(MongoDB) 数据库插入错误
当我使用 NodeJS/Express/Mongoose 执行以下代码时,我在 mongodb 日志中收到错误“Sun Jun 12 15:27:12 SyntaxError: Missing ; before statements (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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据库名称不应包含“.” 。只需删除 .它会工作得很好。
DB name should not contain a '.' . Just remove the . and it will work fine.
@matty我们不应该删除“抛出错误;”
因为如果我们在那里 kke 的话,错误会有 2 个回调
@matty shouldn t we remove the "throw err;"
because there is 2 callback on the error if we kke it there