These are chat archives for Automattic/mongoose

26th
Jun 2018
arbalu
@arbalu
Jun 26 2018 02:51
@Kev , I have issue with mongoose 5.1.6. When result is not on findOne or find, then callback is not returning anything and wait for long time and it fails. Can you help on this
@Kev , I have issue with mongoose 5.1.6. When result is nullon findOne or find, then callback is not returning anything and wait for long time and it fails. Can you help on this
Lamp
@ledlamp
Jun 26 2018 06:56
HALP i'm stumped on a strange bug…
so I query some documents into a variable
and I want to iterate over the array of documents and redefine a buffer to a base64 string
so i can send the list over http with base64-encoded binary
however the weird thing is
the property won't change when i redefine it
i added console.log before and after and they're both buffers, but different
maybe its because the object(s) that comes from await Model.find() is/are a special object with getters/setters or whatever
and it's not letting me modify it/them
Kev
@lineus
Jun 26 2018 09:14
@arbalu I'm happy to take a look
I see the issue you opened, I'll respond there.
Kev
@lineus
Jun 26 2018 10:37

@ledlamp

maybe its because the object(s) that comes from await Model.find() is/are a special object with getters/setters or whatever
and it's not letting me modify it/them

that's correct, you can write an instance method or a virtual getter that returns the base64 string docs here

let me know if you need any help with it
Justin Headley
@JKHeadley
Jun 26 2018 17:22
Hi guys, I'm launching v1 for a project I've been working on for two years now. Please check out the article and let me know what you think :)
https://hackernoon.com/how-to-build-powerful-rest-apis-blazingly-fast-with-node-js-86d6e55a5b34
Kev
@lineus
Jun 26 2018 17:29
@JKHeadley thanks for sharing that. I'll give it a try, it looks cool! :smile: bonus points for the He-Man gif!
Justin Headley
@JKHeadley
Jun 26 2018 17:34
@lineus haha, thanks a lot! :smile:
Lamp
@ledlamp
Jun 26 2018 19:35

@lineus i tried adding a method to convert the buffers

worldSchema.methods.encodeBuffersToBase64 = function() {
    if (this.data) this.data = this.data.toString("base64");
    if (this.meta) this.meta = this.meta.toString("base64");
}

and this is what i have at the other end because it's an array
for (let world of list) world.encodeBuffersToBase64();

but it still doesn't work
how can i just get a simple object i can modify and serialize

i'll try a virtual getter
Kev
@lineus
Jun 26 2018 19:40
heh, I was just writing an example with a virtual getter ;)
Lamp
@ledlamp
Jun 26 2018 19:40
but wait i found what i want http://mongoosejs.com/docs/guide.html#toObject
Kev
@lineus
Jun 26 2018 19:45
are you going to use the transform on toObject?
Lamp
@ledlamp
Jun 26 2018 19:46

this is what I did

var list = await World.find(query, projection);
    list = list.map(doc => {
        doc = doc.toObject();
        if (doc.meta) doc.meta = doc.meta.toString("base64");
        return doc;
    });
    res.send(list);

and it works :smile:

Kev
@lineus
Jun 26 2018 20:08
@ledlamp if you know you won't need any of the mongoose magic on the docs returned from that query, you can add the lean option and just get back an array of POJOs await World.find(query, projection).lean()
Lamp
@ledlamp
Jun 26 2018 20:13
ooh thx