Code Monkey home page Code Monkey logo

Comments (7)

dburles avatar dburles commented on June 15, 2024

Thanks @niklasdahlheimer! all you need to do is apply the helpers to the object, see this example: https://github.com/dburles/meteor-collection-helpers#applying-the-transformation-function

from meteor-collection-helpers.

niklasdahlheimer avatar niklasdahlheimer commented on June 15, 2024

Thanks @dburles, that works for single objects pretty well but I can't make it work with a Meteor collection as param in a function.

function searchWithRegEx(meteorCollection, searchString) {
    return meteorCollection.rawCollection()
        .find({"someKey": new RegExp(searchString, "gi")})
        .toArray()
        .then(array => array.map(result => meteorCollection._transform(result)));
};

Any idea on this? Thanks!

from meteor-collection-helpers.

dburles avatar dburles commented on June 15, 2024

That should result in the methods being applied to each object. However, looking at the use of an arrow function it's likely that calling methods that refer to this might not be correctly referring to the object itself.

from meteor-collection-helpers.

niklasdahlheimer avatar niklasdahlheimer commented on June 15, 2024

I tried

  1. replace the arrow functions with ordinary functions
function searchWithRegEx(meteorCollection, searchString) {
    return meteorCollection.rawCollection()
        .find({"someKey": new RegExp(searchString, "gi")})
        .toArray()
        .then(function (array) {
            array.map(function (result) {
                meteorCollection._transform(result)
            })
        });
};
  1. using call() to pass the collection explicitly as the this object
...
meteorCollection._transform.call(meteorCollection, result);
...

None of that helped.
When I log the passed meteorCollection by calling
console.log(meteorCollection)
it prints out:

SomeCollection {
...
_helpers: [Function: Document]
...
}

So it looks like the helper functions are attached to it. Probably I still have a false understanding of the this keyword, I'm pretty confused why it's not returning the transformed result correctly.

from meteor-collection-helpers.

niklasdahlheimer avatar niklasdahlheimer commented on June 15, 2024

I made some progress on this.

It turns out that meteorCollection._transform(result) indeed adds the correct helpers as prototypes!
The problem is, that my function lives inside a Meteor method (which of course runs serverside) and the results are passed via an async callback-function to the client and that is where the helpers disappear

  • calling Object.getPrototypeOf(myObj) in the Meteor method -> helpers are available
  • calling Object.getPrototypeOf(myObj) in the callback function inside the Meteor.call -> helpers are gone, only default prototypes are shown (constructor, hasOwnProperty,..)

I searched around in the Meteor API reference and Meteor guide but could not find anything related to this issue. Could it be that these helper-methods are not serializable and are thrown away when Meteor passes the results to the client or that Meteor somehow reduces the object to only contain properties?

from meteor-collection-helpers.

dburles avatar dburles commented on June 15, 2024

@niklasdahlheimer Ah I see, yeah that makes sense. That's right, it's just a serialised object being sent across. In that case just apply the helpers to the document returned from the method on the client.

from meteor-collection-helpers.

niklasdahlheimer avatar niklasdahlheimer commented on June 15, 2024

Yes. I will have to do the transformation at a lot of places because the serverside function was meant to be "generic", but there seems to be no other work around.
Because it's likely anothers will stumble across this problem, I would suggest a little note/hint in the Git Readme, but it's yours to decide if it's common knowledge that prototypes are not serializable and couldn't be passed over the wire :)

from meteor-collection-helpers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.