jeff jeff - 4 years ago 508
Javascript Question

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

I'm using pouchDb with Angular2 in a typescript environment with webstorm and systemjs. In the editor, it appears I have sufficent configuration to where I can get intellisense and pouchDB does run.

I have this test code.

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) {

I get the following error

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

I'm thinking it has to do with module loading, I've tried it with an without a value for _id with same result.

Is this a module loading problem and how do I resolve it?

Answer Source

The put method in PouchDb expects a JSON object not a string.

You should just send the object without stringifying it.

var myObj = {"_id" : "1", "description":"hello world"} ;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download