获取集合并为响应添加值 [英] Get a collection and add a value to the response

查看:63
本文介绍了获取集合并为响应添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器脚本中创建一个函数,该函数可以返回一个集合以及一些额外的值. 例如:

I want to create in the Server script a function that can return a collection plus some extra value. For example:

Meteor.publish("users", function () {
    var users;
    users = Meteor.users.find();
    users.forEach(function (user){
        user.profile.image = "some-url";
    });
    return users;
});

但这不能正常工作. 我的问题:在发布功能中向集合响应添加值的正确方法是什么?

But this don't work proper. My question is: What is the right way to add a value to a collection reponse in a publish function.

推荐答案

服务器重要吗?您可以在客户端上使用转换功能:

Is this important to do with the server? You could use the transform function on the client:

客户端JS

//Somewhere where it can run before anything else (make sure you have access to the other bits of the document i.e services.facebook.id otherwise you'll get a services is undefined 

Meteor.users._transform = function(doc) {
    doc.profile.image = "http://graph.facebook.com/" + doc.services.facebook.id + "/picture";
    return doc;
}

现在,当您这样做时:

Meteor.user().profile.image
=> "http://graph.facebook.com/55592/picture"

关于共享到客户端的转换,我已经打开了一个问题: https://github.com/meteor/meteor/issues/821

I have opened an issue before with regards to sharing a transform onto the client: https://github.com/meteor/meteor/issues/821

这篇关于获取集合并为响应添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆