TypeScript 错误 cannot use 'in' operator to search for ‘_id’ for typescript and pouchdb

TypeScript error cannot use 'in' operator to search for '_id' for typescript and pouchdb

我在带有 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 得到相同的结果。

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

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

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

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