an-rahulpandey / cordova-plugin-dbcopy

Copy SQLite Database from www folder to default app database location
Apache License 2.0
89 stars 48 forks source link

TypeError: window.plugins.sqlDB is undefined #13

Closed behzad-azizan closed 9 years ago

behzad-azizan commented 9 years ago

Hello and good times. I use this plugin. I include this scripts to my application :

and copy and past this code : window.plugins.sqlDB.copy("shia.sqlite",success,error);

but after run my application , this error showed in console :

TypeError: window.plugins is undefined window.plugins.sqlDB.copy("shia.sqlite",success,error);

an-rahulpandey commented 9 years ago

What are the entries in your config.xml and cordova_plugins.js file regarding the plugin?

behzad-azizan commented 9 years ago

config.xml :

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.shia.main" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" version="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

cordova_plugins.js :

cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/me.rahul.plugins.sqlDB/www/sqlDB.js",
        "id": "me.rahul.plugins.sqlDB.sqlDB",
        "clobbers": [
            "window.plugins.sqlDB"
        ]
    }
];
module.exports.metadata = 
// TOP OF METADATA
{
    "cordova-plugin-whitelist": "1.0.0",
    "me.rahul.plugins.sqlDB": "1.0.2"
}
// BOTTOM OF METADATA
});

My browser is firefox and I realized that firefox does not support the WebSQL.

When I tested this via chrome , this problem was resolved. But a new problem was born! : Error Code = "Missing Command Error" Uncaught TypeError: Cannot read property 'querySelector' of null in index.js:41

myScript.js :

$(function() {

    dbcopy();
    function dbcopy()
    {
        //Database filename to be copied is demo.db
        window.plugins.sqlDB.copy("shia.sqlite",copysuccess,copyerror);
    }

    function removeDB()
    {
        window.plugins.sqlDB.remove("shia.sqlite",rmsuccess,rmerror);
    }

    function copysuccess()
    {
        //open db and run your queries
        db = window.sqlitePlugin.openDatabase({name: "shia.sqlite"});
        console.log(db);
    }

    function copyerror(e)
    {
        //db already exists or problem in copying the db file. Check the Log.
        console.log("Error Code = "+JSON.stringify(e));
        //e.code = 516 => if db exists
    }

});

index.js :

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();
behzad-azizan commented 9 years ago

The things that I have done :

  1. rename my database to filename.sqlite and copy into \platforms\browser\www
  2. run this command via cmd : cordova serve
  3. run my application via chrome and address localhost:8000/browser/www/
an-rahulpandey commented 9 years ago

The plugin will only work with the device, it copies the database to device directory.

behzad-azizan commented 9 years ago

Hi, I tested this plugin in my phone and my Application crashed after this line executed : window.plugins.sqlDB.copy("shia.sqlite",copysuccess,copyerror); My codes :

 $(function() {

    document.addEventListener("deviceready", onDeviceReady, true);
    function onDeviceReady(){
        dbcopy();
    }
    function dbcopy()
    {
        //Database filename to be copied is demo.db
        alert('befor sqlDB');
        window.plugins.sqlDB.copy("shia.sqlite",copysuccess,copyerror);
        alert('after sqlDB');
    }

    function removeDB()
    {
        window.plugins.sqlDB.remove("shia.sqlite",rmsuccess,rmerror);
    }

    function copysuccess()
    {
        //open db and run your queries
        db = window.sqlitePlugin.openDatabase({name: "shia.sqlite"});
        console.log(db);
        alert('database created');
    }

    function copyerror(e)
    {
        //db already exists or problem in copying the db file. Check the Log.
        console.log("Error Code = "+JSON.stringify(e));
        alert('database error'+JSON.stringify(e));
        //e.code = 516 => if db exists
    }

});
behzad-azizan commented 9 years ago

I realized that something else is problem and create new #14