o2kevin
o2kevin

Reputation: 707

Replace String from MongoDb result using Javascript and Meteor

Okay so for example I have this collection named "Results".

I'll query Results.find();

This will give the value of {foo: 'hello_world'};

and now I want to replace the _ with a " " (white space) using javascript before I return it to the template. Any idea how?

Upvotes: 0

Views: 1296

Answers (2)

Tarang
Tarang

Reputation: 75945

You can use a transform. You just alter your Results.find() to include it as an option.

var transform = function(doc) {
    doc.foo = doc.foo.replace(/_/g, ' ');
    return doc;
}

return Results.find({}, {transform: transform} );

Upvotes: 3

richsilv
richsilv

Reputation: 8013

This should work in your helper:

return Results.find().fetch().map(function(res) {
    res.foo = res.foo.replace('_', ' ');
    return res;
});

Upvotes: 1

Related Questions