【问题标题】:TypeScript error cannot use 'in' operator to search for '_id' for typescript and pouchdbTypeScript 错误无法使用“in”运算符搜索 typescript 和 pouchdb 的“_id”
【发布时间】:2016-04-06 04:27:18
【问题描述】:

我在带有 webstorm 和 systemjs 的打字稿环境中使用 pouchDb 和 Angular2。在编辑器中,我似乎有足够的配置来获得智能感知并且 pouchDB 确实可以运行。

我有这个测试代码。

  var mydb = new PouchDB('TestDb');
  var myObj = {"_id" : "1", "description":"hello world"} ;
  var objAsString = JSON.stringify(myObj) ;
  mydb.put(objAsString).then(function (response) {
      // handle response
      console.log("save to db:" + response) ;
  }).catch(function (err) {
      console.log(err);
  });

我收到以下错误

TypeError: Cannot use 'in' operator to search for '_id' in {"_id":null,"description":"hello world"}
    at PouchDB.<anonymous> (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:210:21)
    at PouchDB.<anonymous> (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:11043:18)
    at PouchDB.<anonymous> (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:5996:21)
    at PouchDB.<anonymous> (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:11043:18)
    at http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:8252:21
    at lib$es6$promise$$internal$$initializePromise (http://localhost:63342/clipsalive/node_modules/angular2/bundles/angular2-polyfills.js:1558:9)
    at new lib$es6$promise$promise$$Promise (http://localhost:63342/clipsalive/node_modules/angular2/bundles/angular2-polyfills.js:1849:9)
    at PouchDB.<anonymous> (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:8239:19)
    at PouchDB.put (http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:11043:18)
    at http://localhost:63342/clipsalive/node_modules/pouchdb/dist/pouchdb.js:5991:32

我认为它与模块加载有关,我已经尝试过没有 _id 的值,结果相同。

这是一个模块加载问题,我该如何解决?

【问题讨论】:

标签: javascript typescript couchdb pouchdb


【解决方案1】:

PouchDb 中的 put 方法需要 JSON 对象而不是字符串。

您应该只发送对象而不对其进行字符串化。

var myObj = {"_id" : "1", "description":"hello world"} ;
mydb.put(myObj)

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 2015-02-19
    • 2013-04-05
    • 2018-07-06
    • 2013-06-10
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多