pouchdb-community / pouchdb-load

Load documents into CouchDB/PouchDB from a dumpfile
Apache License 2.0
120 stars 33 forks source link

how to load json file in pouchdb #49

Closed m0agh closed 8 years ago

m0agh commented 8 years ago

hi,

i have a json file and i want to load these data in my pouchDB in ionic 2. this is my code:

      let PouchDB = require('pouchdb');
      PouchDB.plugin(require('pouchdb-load'));
      initDB() {
      this._db = new PouchDB('product4', { adapter: 'websql' });
       var myDumpedString = [{
        "name": "Product",
        "properties":
        {
            "id":
            {
                "type": "number",
                "description": "Product identifier",
                "required": true
            },
            "name":
            {
                "description": "Name of the product",
                "type": "string",
                "required": true
            },
            "price":
            {
                "type": "number",
                "minimum": 0,
                "required": true
            },
            "tags":
            {
                "type": "array",
                "items":
                {
                    "type": "string"
                }
            }
        }
    }];
    console.log((JSON.stringify(myDumpedString))); 
    this._db.load(myDumpedString).then(function () {
        console.log("Done loading!");
    }).catch(function (err) {
        console.log("error!");
    });

    window["PouchDB"] = PouchDB;
}

when i run this code, i see the json data in console but after that i will get this error:

     GET http://localhost:8100/[object%20Object] 404 (Not Found)
     error!

any help? is this way correct? or i should use another way? Thanks

Pascalmh commented 8 years ago

Your myDumpedString is actually an Array of Objects. Use JSON.stringify - as your do for the console.log.

nolanlawson commented 8 years ago

Actually in this case what you probably want is just db.bulkDocs(). It takes an array of documents and inserts them into PouchDB: https://pouchdb.com/api.html#batch_create

PouchDB load can only be used if you 1) use bulkDocs() to load those documents into a Pouch/Couch somewhere, and then 2) dump from that database, which then dumps a special file containing revision markers and other special Pouch/Couch stuff.

eramudeep commented 3 years ago

i am having json file with 1000000 objects. not sure to upload it into the PouchDB, Need Help