【问题标题】:Where and how to store functions in Meteor? Or how to use system.js?Meteor 在哪里以及如何存储函数?或者如何使用system.js?
【发布时间】:2015-06-15 02:46:00
【问题描述】:

我需要一种方法来存储大量不同的函数,每个函数都有一个唯一的 ID,也许还有一些其他属性,然后我可以查询这些属性并将其带回客户端或服务器以供使用。

保存函数的唯一方法是使用 MongoDB 的 system.js 集合,但我无法通过 Meteor 访问它。

当我尝试在此处实施解决方案时,我似乎可以保存一个函数,但我不知道如何将其恢复并运行它:

How do I access mongos db.system.js in meteor?

// on /server

var myDB = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

systemJS = myDB.collection('system.js');

systemJS.save({
    _id: "echoFunction",
    value : function(x) { return x; }
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

systemJS.save({
    _id : "myAddFunction" ,
    value : function (x, y){ return x + y; }
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

// always causes the server to crash
// systemJS.findOne()

// returns undefined for db
// db.LoadServerScripts()

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    好吧,我找到了一种非常丑陋的方法来让它工作:

    我把它放到 /server/fixtures.js

    myDB = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    
    systemJS = myDB.collection('system.js');
    
    systemJS.save({
        _id: 'echoFunction',
        value : 'function(x) { return x; }'
      },
      function (err, inserted) {
        console.log("error: ", err);
        console.log("number of docs inserted: ", inserted);
      }
    );
    
    systemJS.save({
        _id : "myAddFunction" ,
        value : 'function (x, y){ return x + y; }'
      },
      function (err, inserted) {
        console.log("error: ", err);
        console.log("number of docs inserted: ", inserted);
      }
    );
    
    var arg = "10, 5";
    
    var mongoFuncName='myAddFunction';
    
    var string = 'db.loadServerScripts(); return ' + mongoFuncName+ '(' + arg + ');';
    
    console.log(string);
    
    myDB.eval(string, function (err, mongoFuncReturnData) {
      console.log("error: ", err);
      console.log("mongoFuncReturnData: ", mongoFuncReturnData);
    });
    
    // counting the number of functions in system.js
    myDB.eval("db.system.js.count();", function (err, mongoFuncReturnData) {
      console.log("error: ", err);
      console.log("mongoFuncReturnData: ", mongoFuncReturnData);
    });
    
    // finding and retrieving a stored function via the _id
    myDB.eval("db.system.js.findOne({_id: 'myAddFunction'});", function (err, mongoFuncReturnData) {
      console.log("error: ", err);
      console.log("mongoFuncReturnData: ", mongoFuncReturnData);
    });
    

    天哪,太丑了。我真的希望有更好的方法来做到这一点。

    我还担心这样一个事实,即如果我实施这种明显的 hack,当更新推出时,它可能会在更新时严重损害我的系统。

    还要注意,函数定义仍然必须是字符串。如果它们不是字符串,它们将不会被保存(只有 _id 被保存)。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2012-03-22
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2011-01-05
      相关资源
      最近更新 更多