gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:48:46 25 4
gpt4 key购买 nike

github presentation of minimongo将其声明为

Client-side mongo database with server sync over http



还有一个 minimongo-standalone提供一个 minimongo.min.js ,其中指出:

You could also just download the minimongo.min.js, place it on your server, and reference it in your source.

For browsers

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



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

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

<!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>


它返回错误:
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)

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

最佳答案

一些东西

1. 依赖

如果您阅读 README.MDminimongo-standalone , 它说

Requirements

underscore, async



因此,您需要在 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>

值得一提的是,您需要获取这些库的浏览器版本。看来 async不要使用通用模块定义 (UMD),因此为不同的目标提供单独的文件。

2. 要求

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

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

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

3. minimongo-standaloneminimongo 有一个非常不同的 API

令人沮丧的一点; minimongo-standalone实现 Meteor minimongo 周围的 wrapper ,然后再次重命名它们。
这意味着任何 minimongoMeteor代码不能直接转移。

好的部分是 API 要简单得多。您的示例的等效代码是
// Create collection
var animals = new LocalCollection('animals');
// Insert/update a document

var doc = { species: "dog", name: "Bingo" };
animals.upsert('dog', doc);

// Fetch document and log
var myDog = animals.findOne({ species: "dog" });
console.log("Dog's name is: " + myDog.name);

关于meteor - 如何使 minimongo.js 在浏览器中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com