mongoose save stuck (callback never called)

Course Queries Syllabus Queries 3 years ago

6.93K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


I'm trying to save an mongoose object to db, but it doesn't work (nothing is saved, and no callback returned). The database is open, and there is a successful connection (the collection is even created according to the model, and it indexing it).

my schema:

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': 
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago

This is necromancy :D but maybe it will help someone. I think the issue is with the "pre save" function,

it only returns the callback in one if. If you do not go inside the if a callback is never called.

Adding a

return next();

At the end of the "CourseSchema.pre('save', function(next)" should fix the issue for you.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum