These are chat archives for Automattic/mongoose

15th
Nov 2015
James Lee
@uptownhr
Nov 15 2015 06:22

@vkarpov15 something that should be straight forward but having an issue. Please review.

```

The Site.findOne using doc.site_id is returning null. site == null inside callback.
eventSchema.post('save', function(doc){
  console.log('wtf', doc, doc.site_id, typeof(doc.site_id) )
  Site.findOne({_id: doc.site_id}, function(err, site){
    console.log(err, site)
    site.events.addToSet(doc._id)
    site.save( function(err, saved){
      console.log('saved site event:', err, saved)
    })
  })

})
wtf { status: 'need-review',
  tracking_users: [],
  created_at: Sun Nov 15 2015 06:14:26 GMT+0000 (UTC),
  _id: 564822c29ff782ea00063d5b,
  raw: '',
  url: 'http://www.startuptabs.com',
  text: 'testing',
  name: 'AskMs.Jane',
  site_id: 5647ae80d9d24d01001ae742,
  source: 'twitter',
  __v: 0 } 5647ae80d9d24d01001ae742 object
that's an example output of the console.log('wtf' ...)
the site id exists for sure
James Lee
@uptownhr
Nov 15 2015 06:33
this normally just works ... not sure how to debug this here.
tried wrapping doc.site_id with ObjectId, and no luck
James Lee
@uptownhr
Nov 15 2015 06:54
playing around with this more, certain site_id's do work. in mongodb, they are all stored as ObjectIds. This is really making my head hurt.
James Lee
@uptownhr
Nov 15 2015 07:04
still haven't figured it out but it appears to be related to how I created this event.
let event = new Event({
  site_id: '5631dc9407a36a0100a82cb4',
  text: 'testing again'
})

event.save()
see anything wrong with this?
when looking at the db, the site_id was properly casted to ObjectId.
var eventSchema = new Schema({
  source: {type: String, enum: ['angel','twitter','producthunt','crunchbase']},
  site_id: {type: Schema.Types.ObjectId, ref: 'Site', index: true},
  name: {type: String},
  text: {type: String},
  url: {type: String},
  status: {type: String, default: 'need-review', enum:['need-review', 'approved', 'dismissed']},
  tracking_users: [{type: Schema.Types.ObjectId, ref: 'User'}],
  raw: {type: Schema.Types.Mixed},
  created_at: {type: Date, default: Date.now }
})
James Lee
@uptownhr
Nov 15 2015 07:17
so i if i use a site id that was created a while back, no problems
it's only newly created site_ids
"Site._id"
James Lee
@uptownhr
Nov 15 2015 07:31
lol
i'm such an idiot
i was looking in two different dbs
!!!
Benjamin Atkin
@benatkin
Nov 15 2015 18:46
Is there a way to specify the schema for the values of an object? For example: {"foo": { "subscribed": true, "unread": 9 }, "bar": { "subscribed": false, "unread": 7 } }
If I wanted to put these in an array, I could use [ { "name": String, "subscribed": Boolean, "unread": Number } ].