Wednesday 17 May 2017

Mongoose: pushing new Object schema into array of parent schema creates whole new parent when restarting server on node terminal

Sorry for title gore.

My goal is to have a main parent schema that has all the child sub schema's. When I create a new word I want it to be pushed into the array.

This is working fine but: When I restart the server with the terminal (node server.js) Then navigate to the page where I can submit new words and submit a new word a whole new parent schema ( Document ) is created. The mongoDB is included down below: Test 1 & Test 2 are as planned but when I restarted the server Test 3 was in a new object/parent schema (it needs to be added into the same one - not have a new parent schema created).

My end goal is to have one big document that I can send to my front end and access like: data.words.verbs.english[3] // returns hello (or whatever)

I looked for ages on google but I cant find a solution.

My model . js file

    var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');
var Schema = mongoose.Schema;

var wordObjectSchema = new Schema({
    english: String,
    hanzi: String,
    pinyin: String,
    weight: Number,
    balanceWeight: Number,
    class: String
});
var wordVerbsSchema = new Schema({
    english: String,
    hanzi: String,
    pinyin: String,
    weight: Number,
    balanceWeight: Number,
    class: String
});
var wordsSchema = new Schema({
    objects: [wordObjectSchema],
    verbs: [wordVerbsSchema]

});
var wordVerb = mongoose.model('wordVerb', wordVerbsSchema, 'abc');
var wordObject = mongoose.model('wordObject', wordObjectSchema, 'abc');
var words = mongoose.model('words', wordsSchema, 'abc');


module.exports = {
  wordVerb: wordVerb,
  wordObject: wordObject,
  words: words
};

My routes.js

    var words = new models.words;
app.post('/createWordObject', function (req, res, next) {
    words.objects.push({
    english: req.body.english,
        hanzi: req.body.hanzi,
        pinyin: req.body.pinyin,
        weight: req.body.weight,
        balanceWeight: req.body.balanceWeight,
        class: req.body.class
  })
  words.save(function (err, a) {
    if (err) { return next(err) }
    res.redirect('/main');
  })
})

mongoDB entries

    "_id": {
        "$oid": "591c52fa35c21c27e450f13c"
    },
    "verbs": [],
    "objects": [
        {
            "english": "test today",
            "hanzi": "test today",
            "pinyin": "test today",
            "weight": 123,
            "balanceWeight": 123,
            "class": "test today",
            "_id": {
                "$oid": "591c531335c21c27e450f13d"
            }
        },
        {
            "english": "second test",
            "hanzi": "second test",
            "pinyin": "second test",
            "weight": 1,
            "balanceWeight": 1,
            "class": "second test",
            "_id": {
                "$oid": "591c536135c21c27e450f13e"
            }
        }
    ],
    "__v": 1
}


{
    "_id": {
        "$oid": "591c536fa0615604dcc9f2fe"
    },
    "verbs": [],
    "objects": [
        {
            "english": "third test",
            "hanzi": "third test",
            "pinyin": "third test",
            "weight": 1,
            "balanceWeight": 1,
            "class": "third test",
            "_id": {
                "$oid": "591c5387a0615604dcc9f2ff"
            }
        }
    ],
    "__v": 0
}



via Michael Baumgarn

No comments:

Post a Comment