Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.88k stars 3.83k forks source link

New model from object doesn't retain all object data #4205

Closed sabrehagen closed 8 years ago

sabrehagen commented 8 years ago

Hey Val,

I have a request body which I supply to the creation of a model. The projects property that is on the request body is not transferred to the new model. The projects property is exactly the same as the fields property which is transferred to the new model. I've supplied the request body, the thread model (no toJSON or toObject transforms present), and the schema below.

Why isn't the projects property on the thread model reflecting the data supplied in the request body?

const thread = new req.app.models.project(req.body);
console.log('req.body\n\n', req.body)    console.log('============================================================================')
console.log('thread\n\n', thread)

OUTPUT:

req.body

{ _id: '575357020192c84c22c4ca91',
  stub: 'brandens-blog',
  name: 'branden\'s blog',
  owner: 
   { _id: '55963596423fe3ab0f034ded',
     stub: 'brandenshort52',
     name: 'Branden Short',
     numProjects: 0,
     followers: 20,
     profile: { banner: [Object], lastname: 'Short', firstname: 'Branden' },
     blurb: 'Full time engineer | Part time space lover',
     picture: '/uploads/0e94a25f-b10f-4e64-9e53-13f12800dce3.jpeg',
     type: 'user',
     entityType: 'user' },
  __v: 0,
  numMentions: 0,
  numPosts: 0,
  wordCount: 1,
  followers: 0,
  views: 0,
  answered: false,
  deleted: false,
  flagged: false,
  published: true,
  likes: 0,
  submitted: '2016-06-04T22:32:45.527Z',
  updated: '2016-06-04T23:20:53.593Z',
  sectionData: 
   { sections: { '950b2165ffd0abe4c873a63d': [Object] },
     sectionOrder: [ '950b2165ffd0abe4c873a63d' ] },
  type: 'blog',
  organisations: [],
  fields: 
   [ { _id: '5521fbe89fbbb77c26244c2d',
       stub: 'space',
       name: 'Space',
       blurb: 'Outer space, or just space, is the void that exists between celestial bodies, including the Earth. It is not completely empty, but consists of a hard vacuum containing a low density of particles, predominantly a plasma of hydrogen and helium as well as electromagnetic radiation, magnetic fields, neutrinos, dust and cosmic rays.',
       picture: '/uploads/bb8f1af2-706c-463f-84cb-0f62cfc1cd4d.jpeg',
       type: 'field',
       entityType: 'field' },
     { _id: '5521fbe89fbbb77c26244c18',
       stub: 'satellites',
       name: 'Satellites',
       blurb: 'In the context of spaceflight, a satellite is an artificial object which has been intentionally placed into orbit.',
       picture: '/uploads/81866710-7d9d-49c8-b56c-d5b499326d78.jpeg',
       type: 'field',
       entityType: 'field' },
     { _id: '56b32fec0d891bf62772bdaa',
       stub: 'computer-aided-design-cad',
       name: 'Computer-Aided Design (CAD)',
       numGenerals: 0,
       numQuestions: 0,
       numBlogs: 1,
       numThreads: 1,
       numProjects: 94,
       followers: 60,
       banner: [Object],
       blurb: 'Computer-aided design (CAD) is the use of computer systems to aid in the creation, modification, analysis, or optimization of a design.',
       picture: '',
       type: 'field',
       entityType: 'field' } ],
  projects: 
   [ { _id: '575357390192c84c22c4dfe4',
       stub: 'brandens-project',
       name: 'branden\'s project',
       numComments: 0,
       wordCount: 0,
       created: '2016-06-04T22:33:29.226Z',
       updated: '2016-06-04T22:33:29.225Z',
       team: [Object],
       likes: 0,
       views: 0,
       organisations: [],
       fields: [],
       blurb: '',
       picture: '',
       posts: [],
       type: 'project',
       entityType: 'project' },
     { _id: '547db55af7f342380174e212',
       name: 'FlowerSat1 - CO2 Sensing CubeSat',
       stub: 'flowersat1---c02-sensing-cubesat',
       numComments: 2,
       numGenerals: 0,
       numQuestions: 1,
       numBlogs: 0,
       numThreads: 1,
       wordCount: 2706,
       created: '2015-01-15T00:00:00.000Z',
       updated: '2016-02-25T10:42:25.659Z',
       team: [Object],
       likes: 7,
       followers: 7,
       views: 1834,
       organisations: [Object],
       fields: [Object],
       blurb: 'Flower Sat 1 is a concept satellite that was designed to detect carbon dioxide levels across continental Australia.',
       picture: '/uploads/upload_19d2b1a10bd45ed83453cdc7350899df.JPG',
       posts: [],
       type: 'project',
       entityType: 'project' } ],
  banner: '',
  body: '',
  blurb: '',
  picture: '',
  entityType: 'thread' }
============================================================================
thread

{ picture: '',
  blurb: '',
  summary: '',
  banner: '',
  fields: 
   [ 5521fbe89fbbb77c26244c2d,
     5521fbe89fbbb77c26244c18,
     56b32fec0d891bf62772bdaa ],
  organisations: [],
  location: [],
  views: 0,
  followers: 0,
  likes: 0,
  published: true,
  license: 'CC BY',
  shares: 0,
  slider: [],
  team: [],
  status: 4,
  updated: 2016-06-04T23:20:53.593Z,
  created: 2016-06-04T23:21:24.467Z,
  stemnscore: 0,
  sectionData: 
   { sectionOrder: [ '950b2165ffd0abe4c873a63d' ],
     sections: { '950b2165ffd0abe4c873a63d': [Object] } },
  socialLinks: 
   { website: '',
     twitter: '',
     facebook: '',
     linkedin: '',
     google: '',
     github: '',
     stack: '',
     youtube: '' },
  wordCount: 1,
  numThreads: 0,
  numBlogs: 0,
  numQuestions: 0,
  numGenerals: 0,
  numComments: 0,
  numMentions: 0,
  remote: { provider: '', token: '', id: '', root: '' },
  __v: 0,
  name: 'branden\'s blog',
  stub: 'brandens-blog',
  _id: 575357020192c84c22c4ca91 }

Schema

const threadSchema = Schema({
    name           :  { type : String                             },
    stub           :  { type : String                             },
    picture        :  { type : String,   default : ''             },
    blurb          :  { type : String,   default : ''             },

    body           :  { type : String,   default : ''             },
    banner         :  { type : String,   default : ''             },

    projects       : [{ type : ObjectId, ref     : 'Project'      }],
    fields         : [{ type : ObjectId, ref     : 'Field'        }],
    organisations  : [{ type : ObjectId, ref     : 'Organisation' }],
    type           :  { type : String,   default : ''             },

    sectionData        : {
        sectionOrder   : [{ type : String                         }],
        sections       : {  type : Mixed,                         }
    },

    owner          :  { type : ObjectId, ref     : 'User'         },
    posts          : [{ type : ObjectId, ref     : 'Post'         }],
    updated        :  { type : Date,     default : Date.now       },
    submitted      :  { type : Date,     default : Date.now       },
    likes          :  { type : Number,   default : 0              },
    published      :  { type : Boolean,  default : false          },
    flagged        :  { type : Boolean,  default : false          },
    deleted        :  { type : Boolean,  default : false          },
    answered       :  { type : Boolean,  default : false          },
    views          :  { type : Number,   default : 0              },
    followers      :  { type : Number,   default : 0              },
    lastPoster     :  { type : ObjectId, ref     : 'User'         },
    wordCount      :  { type : Number,   default : 0              },
    numPosts       :  { type : Number,   default : 0              },
    numMentions    :  { type : Number,   default : 0              }
});
vkarpov15 commented 8 years ago

Hmm can you clarify which version of mongoose you're using and which version of node? Below script works fine for me:

'use strict';

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

var ObjectId = Schema.Types.ObjectId;
var Mixed = Schema.Types.Mixed;

mongoose.connect('mongodb://localhost/gh4205');
mongoose.set('debug', true);

const threadSchema = Schema({
    name           :  { type : String                             },
    stub           :  { type : String                             },
    picture        :  { type : String,   default : ''             },
    blurb          :  { type : String,   default : ''             },

    body           :  { type : String,   default : ''             },
    banner         :  { type : String,   default : ''             },

    projects       : [{ type : ObjectId, ref     : 'Project'      }],
    fields         : [{ type : ObjectId, ref     : 'Field'        }],
    organisations  : [{ type : ObjectId, ref     : 'Organisation' }],
    type           :  { type : String,   default : ''             },

    sectionData        : {
        sectionOrder   : [{ type : String                         }],
        sections       : {  type : Mixed,                         }
    },

    owner          :  { type : ObjectId, ref     : 'User'         },
    posts          : [{ type : ObjectId, ref     : 'Post'         }],
    updated        :  { type : Date,     default : Date.now       },
    submitted      :  { type : Date,     default : Date.now       },
    likes          :  { type : Number,   default : 0              },
    published      :  { type : Boolean,  default : false          },
    flagged        :  { type : Boolean,  default : false          },
    deleted        :  { type : Boolean,  default : false          },
    answered       :  { type : Boolean,  default : false          },
    views          :  { type : Number,   default : 0              },
    followers      :  { type : Number,   default : 0              },
    lastPoster     :  { type : ObjectId, ref     : 'User'         },
    wordCount      :  { type : Number,   default : 0              },
    numPosts       :  { type : Number,   default : 0              },
    numMentions    :  { type : Number,   default : 0              }
});

const body = { _id: '575357020192c84c22c4ca91',
  stub: 'brandens-blog',
  name: 'branden\'s blog',
  owner:
   { _id: '55963596423fe3ab0f034ded',
     stub: 'brandenshort52',
     name: 'Branden Short',
     numProjects: 0,
     followers: 20,
     profile: { banner: [Object], lastname: 'Short', firstname: 'Branden' },
     blurb: 'Full time engineer | Part time space lover',
     picture: '/uploads/0e94a25f-b10f-4e64-9e53-13f12800dce3.jpeg',
     type: 'user',
     entityType: 'user' },
  __v: 0,
  numMentions: 0,
  numPosts: 0,
  wordCount: 1,
  followers: 0,
  views: 0,
  answered: false,
  deleted: false,
  flagged: false,
  published: true,
  likes: 0,
  submitted: '2016-06-04T22:32:45.527Z',
  updated: '2016-06-04T23:20:53.593Z',
  sectionData:
   { sections: { '950b2165ffd0abe4c873a63d': [Object] },
     sectionOrder: [ '950b2165ffd0abe4c873a63d' ] },
  type: 'blog',
  organisations: [],
  fields:
   [ { _id: '5521fbe89fbbb77c26244c2d',
       stub: 'space',
       name: 'Space',
       blurb: 'Outer space, or just space, is the void that exists between celestial bodies, including the Earth. It is not completely empty, but consists of a hard vacuum containing a low density of particles, predominantly a plasma of hydrogen and helium as well as electromagnetic radiation, magnetic fields, neutrinos, dust and cosmic rays.',
       picture: '/uploads/bb8f1af2-706c-463f-84cb-0f62cfc1cd4d.jpeg',
       type: 'field',
       entityType: 'field' },
     { _id: '5521fbe89fbbb77c26244c18',
       stub: 'satellites',
       name: 'Satellites',
       blurb: 'In the context of spaceflight, a satellite is an artificial object which has been intentionally placed into orbit.',
       picture: '/uploads/81866710-7d9d-49c8-b56c-d5b499326d78.jpeg',
       type: 'field',
       entityType: 'field' },
     { _id: '56b32fec0d891bf62772bdaa',
       stub: 'computer-aided-design-cad',
       name: 'Computer-Aided Design (CAD)',
       numGenerals: 0,
       numQuestions: 0,
       numBlogs: 1,
       numThreads: 1,
       numProjects: 94,
       followers: 60,
       banner: [Object],
       blurb: 'Computer-aided design (CAD) is the use of computer systems to aid in the creation, modification, analysis, or optimization of a design.',
       picture: '',
       type: 'field',
       entityType: 'field' } ],
  projects:
   [ { _id: '575357390192c84c22c4dfe4',
       stub: 'brandens-project',
       name: 'branden\'s project',
       numComments: 0,
       wordCount: 0,
       created: '2016-06-04T22:33:29.226Z',
       updated: '2016-06-04T22:33:29.225Z',
       team: [],
       likes: 0,
       views: 0,
       organisations: [],
       fields: [],
       blurb: '',
       picture: '',
       posts: [],
       type: 'project',
       entityType: 'project' },
     { _id: '547db55af7f342380174e212',
       name: 'FlowerSat1 - CO2 Sensing CubeSat',
       stub: 'flowersat1---c02-sensing-cubesat',
       numComments: 2,
       numGenerals: 0,
       numQuestions: 1,
       numBlogs: 0,
       numThreads: 1,
       wordCount: 2706,
       created: '2015-01-15T00:00:00.000Z',
       updated: '2016-02-25T10:42:25.659Z',
       team: [Object],
       likes: 7,
       followers: 7,
       views: 1834,
       organisations: [Object],
       fields: [Object],
       blurb: 'Flower Sat 1 is a concept satellite that was designed to detect carbon dioxide levels across continental Australia.',
       picture: '/uploads/upload_19d2b1a10bd45ed83453cdc7350899df.JPG',
       posts: [],
       type: 'project',
       entityType: 'project' } ],
  banner: '',
  body: '',
  blurb: '',
  picture: '',
  entityType: 'thread' };

const Thread = mongoose.model('Thread', threadSchema);
const thread = new Thread(body);
console.log('thread\n\n', thread)

Output:

$ node gh-4205.js 
thread

 { picture: '',
  blurb: '',
  body: '',
  banner: '',
  projects: [ 575357390192c84c22c4dfe4, 547db55af7f342380174e212 ],
  fields: 
   [ 5521fbe89fbbb77c26244c2d,
     5521fbe89fbbb77c26244c18,
     56b32fec0d891bf62772bdaa ],
  organisations: [],
  type: 'blog',
  sectionData: 
   { sectionOrder: [ '950b2165ffd0abe4c873a63d' ],
     sections: { '950b2165ffd0abe4c873a63d': [Object] } },
  posts: [],
  updated: 2016-06-04T23:20:53.593Z,
  submitted: 2016-06-04T22:32:45.527Z,
  likes: 0,
  published: true,
  flagged: false,
  deleted: false,
  answered: false,
  views: 0,
  followers: 0,
  wordCount: 1,
  numPosts: 0,
  numMentions: 0,
  __v: 0,
  owner: 55963596423fe3ab0f034ded,
  name: 'branden\'s blog',
  stub: 'brandens-blog',
  _id: 575357020192c84c22c4ca91 }
vkarpov15 commented 8 years ago

Issue's gone stale, closing.

sabrehagen commented 8 years ago

Sorry for not getting in sooner. I was using latest mongoose with latest node as of today.

vkarpov15 commented 8 years ago

No worries. Are you still getting this issue? If so, can you try modifying the script provided to repro your issue?