如何使minimongo.js在浏览器中工作?

时间:2018-02-21 16:33:27

标签: meteor minimongo

github presentation of minimongo将其声明为

  

客户端mongo数据库,服务器通过http同步

还有一个minimongo-standalone提供了一个minimongo.min.js,其中说明:

  

你也可以下载minimongo.min.js,把它放在你的   服务器,并在您的来源中引用它。

     

对于浏览器

     

<script src="/js/minimongo.min.js"></script>

我之前使用的d3.js是以某种方式打包的,因此.js文件在Web浏览器中作为lib工作,在节点上作为npm包工作。

所以我尝试使用新下载的minimongo.js本地在Chrome中使用indexeddb构建一个经典网页,就像我对D3.js一样。它提供了类似的东西(jsfiddle):

&#13;
&#13;
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>MiniMongo</title>
    <script src="https://rawgit.com/rurri/minimongo-standalone/master/minimongo.min.js"></script>
    <!-- https://github.com/rurri/minimongo-standalone/blob/master/minimongo.min.js -->
  </head>
  <body></body>
  <script>
    // Require minimongo
    var minimongo = require("minimongo");
    var LocalDb = minimongo.MemoryDb;
    // Create local db (in memory database with no backing)
    db = new LocalDb();
    // Add a collection to the database
    db.addCollection("animals");
    doc = { species: "dog", name: "Bingo" };
    // Always use upsert for both inserts and modifies
    db.animals.upsert(doc, function() {
      // Query dog (with no query options beyond a selector)
      db.animals.findOne({
        species: "dog"
      }, {}, function(res) {
        console.log("Dog's name is: " + res.name);
      });
    });

  </script>
</html>
&#13;
&#13;
&#13;

它返回错误:

Uncaught ReferenceError: _ is not defined
    at minimongo.min.js:1
    at minimongo.min.js:3
Uncaught ReferenceError: require is not defined
    at (index):5911
Uncaught ReferenceError: _ is not defined
    at (index):91
    at window.onload ((index):5889)

我错过了什么或误解了什么? 如果可能,如何使它工作?

2 个答案:

答案 0 :(得分:4)

一些事情

1。依赖

如果您阅读README.MD的{​​{1}},则说明

  

要求

     

下划线,异步

因此,您需要在minimongo-standalone脚本标记之前在页面上包含这两个库。

minimongo

值得一提的是,您需要获取这些库的浏览器版本。似乎<head> <script src="https://link/to/underscore.js"></script> <script src="https://link/to/async.js"></script> <script src="https://rawgit.com/rurri/minimongo-standalone/master/minimongo.min.js"></script> 没有使用通用模块定义(UMD),因此为不同的目标提供单独的文件。

2。需要

除非您使用browserify或其他commonJS模块加载框架,否则函数async不存在。

我还没有检查requireasync,但如果模块系统不存在,大多数库都会回退到浏览器中的普通全局变量。

在包含三个脚本标记后,您应该能够全局访问minimongo-standalone的导出符号

3。 underscoreminimongo-standalone

的API非常不同

令人沮丧的一点; minimongo实现围绕minimongo-standalone的{​​{1}}包装器,然后再次重命名它们。 这意味着任何Meteorminimongo代码都不能直接转让。

优点是API更简单。您的示例的等效代码将是

minimongo

答案 1 :(得分:1)

@Fred_Stark:我们鼓励您将其整合到您的答案中。

短:工作但超重(800kb!)。 https://jsfiddle.net/7fehe9ey/9/。使用别的东西!

初始代码

/* **************************************************************** */
/* THIS WILL GO TO BUNDLE ***************************************** */
// Require minimongo
var minimongo = require('minimongo');
var LocalDb = minimongo.MemoryDb;
// Create local db (in memory database with no backing)
db = new LocalDb();

/* **************************************************************** */
/* THIS WILL GO TO INDEX.HTML ************************************* */
// Add a collection to the database
db.addCollection("animals");
doc = { species: "dog", name: "Bingo" };
// Always use upsert for both inserts and modifies
db.animals.upsert(doc, function() {
  // Query dog (with no query options beyond a selector)
  db.animals.findOne({
    species: "dog"
  }, {}, function(res) {
    console.log("Dog's name is: " + res.name);
  });
});

捆绑

npm install minimongo underscore async
npm install -g browserify
echo "var minimongo=require('minimongo');var LocalDb=minimongo.MemoryDb;db=new LocalDb();" > ./app.js
browserify ./app.js -o ./bundle.js

它产生一个800Kb ./bundle.js文件,在jsfiddle中用作黑盒子jsfiddle.net/7fehe9ey/8

HTML-JS

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MiniMongo</title>
    <!-- ======================================================= -->
    <!-- ======================================================= -->
    <script src="./js/bundle.js"></script><!--================== -->
    <!-- ======================================================= -->
    <!-- ======================================================= -->
  </head>
  <body>
</body>
    <script>
    // Require minimongo
    // var minimongo = require('minimongo');
    // var LocalDb = minimongo.MemoryDb;
    // Create local db (in memory database with no backing)
    // db = new LocalDb();
    // Add a collection to the database
    db.addCollection("animals");
    doc = { species: "Cat", name: "Cingo" };
    // Always use upsert for both inserts and modifies
    db.animals.upsert(doc, function() {
      // Query dog (with no query options beyond a selector)
      db.animals.findOne({
        species: "Cat"
      }, {}, function(res) {
        console.log("Cat's name is: " + res.name);
      });
    });

    </script>
</html>

返回

控制台返回:

  

猫的名字是:Cingo