Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this simple Mongoose.js program freeze while doing a 'nested' save?
    primarykey
    data
    text
    <p><strong>[edit]: I updated the previous question with this, using a very specific reproducible example.</strong></p> <p>This is my entire program. <br><br> I create two Schemas <code>ASchema</code> and <code>BSchema</code> with collections <code>A</code> and <code>B</code> respectively, make two objects <code>a</code> and <code>b</code>, and attempt to save them sequentially - that is, first <code>a</code> then <code>b</code>.</p> <pre><code>mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/test'); ASchema = new Schema({ text: String }); BSchema = new Schema({ val: Number }); A = mongoose.model('A', ASchema); B = mongoose.model('B', BSchema); a = new A({text: 'this is a'}); b = new B({val: 5}); a.save(function(err) { if(err) console.log(err.message); else { console.log('saved a : ', a); b.save(function(err) { if(err) console.log(err.message); else { console.log('saved b : ', b); } }); } }); mongoose.disconnect(); </code></pre> <p><strong>What I expect should happen:</strong> <br>It should print <code>saved a :</code> followed by the document a, and then <code>saved b :</code>, followed by the document b.<br><br> <strong>What actually happens:</strong> <br>It prints <code>saved a : { text: 'this is a', _id: 4ee4cab00d2c35fc04000001 }</code> and nothing more. The program does not stop either; it stays 'stuck'.<br> Looking through the mongo shell, I find that a collection <code>as</code> (<code>a</code> pluralized by mongoose, that is okay) has been created, and I can see saved document in it with <code>db.as.find()</code>. However, I cannnot find a collection <code>bs</code>.<br><br> <strong>Tweak:</strong> <br>In the saving code, swapping the places of <code>a</code> and <code>b</code> (order of saving) causes <code>b</code> to be saved, and <code>a</code> to not be saved. So the problem is not specifically with <code>a</code> or <code>b</code>.<br><br> <strong>Question:</strong> Why does it not save the next document?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload