These are chat archives for Automattic/mongoose

11th
Aug 2015
Matt Murphy
@mmmurf
Aug 11 2015 00:18
hello, i'm using mongoose and one of my models has this schema def: {type: mongoose.Schema.Types.ObjectId, ref: "User" }, ... and i'm wondering if
I can call populate() and have it populate the ref automatically
Luke A. Greenleaf
@gishmel
Aug 11 2015 00:23
what do you mean automatically populate? I believe the short answer is no
you can do a method on pre save to unpopulate your schemas and save just the ids and similarly you can do a populate on getting from the database
but you need to write those methods at least thats my understanding I would be very grateful if you found a way to automatically resolve references depending on saving or getting from the database
Matt Murphy
@mmmurf
Aug 11 2015 00:48
gishmel: ok
Jeff Harris
@techjeffharris
Aug 11 2015 08:39
The purpose of using a ref is similar to that of an SQL foreign key. It is used to identify a document in another collection, so that you don't store the data in two places. Populating a document only affects the returned query result after it has been retrieved. It does not affect the document inside the collection. If you want the data in only one document, rather than another collection, you should nest the data and get
Jeff Harris
@techjeffharris
Aug 11 2015 08:55
I guess there is a character limit to messages here... Not sure what the rest of that sentence said, but... If it's only relevant in one place, embed the data (a subdocument), but if more than one document uses the data, you probably want to make a model for that type of data and store the _id in each document that needs it.
Fayi Femi-Balogun
@fayimora
Aug 11 2015 22:29
I’m trying to write an id to to firebase but it keeps writing jibberish. Basically, I have a reference in a Delivery model that looks like this:
customer: {type: Schema.ObjectId, ref: 'User’}
Now when I write delivery.customer to console, I get the right id but when I do the same thing to firebase I get something like this:
customer_id: { _bsontype: ObjectID, id: U~êtdWdYë }
Any idea what the problem is?
Fayi Femi-Balogun
@fayimora
Aug 11 2015 22:39
I get the same thing when i print “delivery.customer.id” to the console :cry:
Maksim
@chetverikov
Aug 11 2015 23:37
Use .id.toString()
Fayi Femi-Balogun
@fayimora
Aug 11 2015 23:49
@chetverikov yeah i tried that but to no avail
still got the gobledegook string
Yassine Sania
@devey
Aug 11 2015 23:58
Hi guys, it's right to write something like that
'List.findById(id, function(err, list){
if (err){
res.send(err);
}
for (i = 0; i < list.items.length; i++ ){
if (err){
res.send(err);
}
List.findById(list.items[i], function(err, item){
list.items[i] = item;
});
}
res.json({list: list});
});'