Comments (7)
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.
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.
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.
I tried
- 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)
})
});
};
- 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.
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.
@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.
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)
- helper collection based on array, is this possible? HOT 1
- Transition to npm? HOT 1
- Could I use with Meteor 1.4? HOT 4
- TypeError: <Collection>.helpers is not a function when running package tests HOT 4
- printing the complete function on to the console instead of returning the value HOT 2
- Collection helpers problem HOT 1
- Collections operations breaking when adding a collection helper in a FileCollection (and potentially in any Mongo.Collection instance) HOT 19
- Meteor helpers with fetch HOT 4
- Trying to work with a imported lib inside the helper
- Collection.helpers are not defined HOT 3
- findOne return Object with "Document" metadata HOT 3
- Ecmascript problem HOT 3
- ES6 Getters/Setters support HOT 4
- Test collection helpers and stub them while testing other things HOT 2
- Issues with collection helpers on Meteor 1.5? HOT 3
- Helpers in React containers sometimes missing HOT 1
- Collection helper not loaded with data. HOT 8
- How can I use this package in my angular2-meteor application?
- How to initialize @types/meteor-dburles-collection-helpers? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-collection-helpers.