mongoose save stuck(回调永远不会被调用)

时间:2016-01-07 22:15:28

标签: javascript node.js mongodb mongoose save

我正在尝试将mongoose对象保存到db,但它不起作用(没有保存,也没有返回回调)。数据库是打开的,并且连接成功(甚至根据模型创建集合,并将其编入索引)。

我的架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CourseSchema = new Schema({
  providerId: {type: String, required: true, unique: true},
  from: {
    provider: {type: String, required: true},
    creators: [String]
  },
  name: {type: String, required: true},
  link: {type: String, required: true},
  textualInfo:{
    summery: String,
    longDescription: String,
    syllabus: String,
    subjects: [String]
  },
  media: {
    image: String,
    trailer: String
  },
  meta:{
    languages: [String],
    level: Number,
    workload: String,
    duration: String,
    prerequirments: String,
    priceDollars: Number,
    enrollments: Number,
  },
  dateCreated: {type: Date, default: Date.now},
  dateLocalModified: {type: Date},
  queries: [String]
});

CourseSchema.index({
  'from.provider': 'text',
  'from.creators': 'text',
  'name': 'text',
  'textualInfo.summery': 'text',
  'textualInfo.longDescription': 'text',
  'textualInfo.syllabus': 'text',
  'textualInfo.subjects': 'text'
},{
  name: 'search index', weights: {
  'from.provider': 120,
  'from.creators': 120,
  'name': 150,
  'textualInfo.summery': 20,
  'textualInfo.longDescription': 2,
  'textualInfo.syllabus': 1,
  'textualInfo.subjects': 20
}});

CourseSchema.pre('save', function(next) {
  var course = this;
  course.dateLocalModified = new Date();

  if(!course.isModified('providerId')) return next();
  if(!(/@/.test(course.providerId))){
    course.providerId = course.providerId + '@' + course.from.provider;
  }
});


var Course = mongoose.model('Course', CourseSchema);
module.exports = Course;

插入测试代码:

    var Course = require('./course');
var mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/mydb',{
  server: {socketOptions: { keepAlive: 1 }},
  replset: {socketOptions: { keepAlive: 1 }}
});

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
  console.log('mongoose is connected to db');
  var course = new Course({
    providerId: 'pid123',
      from: {
        provider: 'test',
        creators: ['creators']
      },
      name: 'test course',
      link: 'http://testlink.com',
      textualInfo:{
        summery: 'c.shortDescription',
        longDescription: 'c.aboutTheCourse',
        syllabus: 'c.courseSyllabus',
        subjects: ['subjNames'],
      },
      media: {
        image: 'c.photo',
        trailer:  "https://www.youtube.com/watch?v="
      },
      meta:{
        languages: [],
        level: 0,
        workload: 'c.estimatedClassWorkload',
        duration: "",
        prerequirments: 'c.recommendedBackground',
        priceDollars: 0,
        enrollments: 0,
      },
      queries: []
  });
  console.log('trying to save:', course.name);
  course.save(function(err){
    console.log(err);
    db.close();
  });
});
db.on('disconnected', function () {
  console.log('Mongoose default connection disconnected');
});

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是巫术:D,但也许会帮助某人。 我认为问题在于“预保存”功能,

它仅在一个条件中返回回调。如果不进入,则永远不会调用回调。

添加

return next();

在“ CourseSchema.pre('save',function(next)”)的结尾应为您解决此问题。