Meteor,Mongo通过函数问题插入字段值

时间:2015-10-08 16:44:25

标签: mongodb meteor insert

我是Mongo的新手,所以也许这是愚蠢的Q,但我无法在网上找到类似的问题。当我尝试插入具有在函数中计算的字段值的文档时,字段根本不在对象中。结果是这样的

var token = function(){
  return "xxxyyy";
}

var companyId = Companies.insert({
 name: company.name,
 email: company.email,
 country: company.country,
 city: company.city,
 token: function(){
        return token();
        }
});

Result is this document:
{
    "_id" : "fm36vAH58787fJq66",
    "name" : "TestCo",
    "email" : "email@email.com",
    "country" : "",
    "city" : ""

}

1 个答案:

答案 0 :(得分:3)

您真的想要JSON中的函数还是只需要结果?如果你想要结果,试试这个:

var token = function(){
  return "xxxyyy";
}

var companyId = Companies.insert({
 name: company.name,
 email: company.email,
 country: company.country,
 city: company.city,
 token: token()
});