【发布时间】:2016-08-02 13:55:56
【问题描述】:
我目前使用 PouchDB 作为我的数据库,并且我使用 Cloudant 作为远程服务。我目前正在尝试创建文档,但是,当我调用该函数时,出现错误。
我可以知道我哪里做错了吗?可能是网址错误还是我的语法错误?
未捕获的引用错误:PouchDB 不是构造函数
这是我的 JavaScript 代码
function pouchdb() {
var db = new PouchDB("todos");
var remoteDB = new PouchDB("http://example.cloudant.com/example");
window.PouchDB = db;
var doc = {
"_id": "Can123",
"name": "You123",
"occupation": "See1",
"age": 3,
"hobbies": [
"Watch 9pm show",
"chasing laser pointers",
"lookin' hella cute"
]
};
db.put(doc);
PouchDB.sync(db, remoteDB);
}
HTML 代码
<button onclick="pouchdb()">pouchdb</button>
更新
我更改了这组代码的插入代码
function pouchdb() {
var db = new PouchDB("todos");
var remoteDB = new PouchDB("http://example.cloudant.com/example");
var todo = {
_id: "mittens1233",
title: "hello",
occupation: "kitten123"
};
db.put(todo, function callback(err, result) {
if (!err) {
console.log('Successfully posted a todo!');
}
});
}
我得到的结果是 Successfully posted a todo!,但是,我的 cloudant 仪表板仍然显示 0 doc。我可以知道为什么吗?
【问题讨论】:
-
PouchDB不是构造函数。在new PouchDB(...)中取消声明new。 -
@TheProHands 所以应该是
var db = PouchDB("todos");var remoteDB = PouchDB("http://example.cloudant.com/example");? -
好吧,如果那是错误,那是对的,但是库在我看到它的页面后说它是一个构造函数。
-
@TheProHands 我看过其他页面的例子,他们也做过
new PouchDB(...)..所以我非常怀疑这是错误吗? -
PouchDB 是否加载正确? (顺便说一句,你应该避免使用
window.PouchDB = db;,因为window.PouchDB已经被用于 PouchDB 函数,并且覆盖全局变量会导致奇怪的错误。)
标签: javascript sql pouchdb cloudant