Closed sabrehagen closed 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 }
Issue's gone stale, closing.
Sorry for not getting in sooner. I was using latest mongoose with latest node as of today.
No worries. Are you still getting this issue? If so, can you try modifying the script provided to repro your issue?
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. Theprojects
property is exactly the same as thefields
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?Schema