TypeScript错误无法使用' in'运营商搜索' _id' for typescript和pouchdb

时间:2016-01-01 19:28:55

标签: javascript typescript couchdb pouchdb

我在带有webstorm和systemjs的打字稿环境中使用带有Angular2的pouchDb。在编辑器中,似乎我有足够的配置,我可以获得intellisense和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的值并且结果相同。

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

1 个答案:

答案 0 :(得分:1)

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

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

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