【问题标题】:ionic 2 Offline Data Sync using Azure Mobile Apps throws TypeError:Cannot read property 'openDatabase' of undefinedionic 2 Offline Data Sync using Azure Mobile Apps throws TypeError:Cannot read property 'openDatabase' of undefined
【发布时间】:2018-05-16 11:15:13
【问题描述】:

我使用 Visual Studio 模板创建了 ionic 2 应用程序,并尝试在 Azure 移动应用程序功能中开发离线数据同步功能。 我已经安装了节点“azure-mobile-apps-client 模块”和 在 app.components.ts 中使用 import * as WindowsAzure from 'azure-mobile-apps-client'; 并使用初始化存储 Client = new WindowsAzure.MobileServiceClient("url"); 但错误显示我为“TypeError:Cannot read property 'openDatabase' of undefined”。

我还安装了@ionic-native/sqlite 节点模块和cordova-sqlite-storage 插件。

请看下面的代码:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { NavController } from 'ionic-angular';
//declare var WindowsAzure: any;
import * as WindowsAzure from 'azure-mobile-apps-client';

var mobileAppClient, // Connection to the Azure Mobile App backend
    store,  // Sqlite store to use for offline data sync
    syncContext, // Offline data sync context
    tableName
var useOfflineSync: boolean = true;


@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})
export class AboutPage {

    constructor(public navCtrl: NavController, public platform:Platform) {

        platform.ready().then(() => {
            mobileAppClient = new WindowsAzure.MobileServiceClient("https://myapp.azurewebsites.net");
            // Create the sqlite store
            store = new WindowsAzure.MobileServiceSqliteStore('store.db');

            store.defineTable({
                name: 'todoitem',
                columnDefinitions: {
                    id: 'string',
                    text: 'string',
                    complete: 'boolean',
                    version: 'string'
                }
            });

            // Get the sync context from the client
            syncContext = mobileAppClient.getSyncContext();

            // Initialize the sync context with the store\
            syncContext.initialize(store).then((syc) => {

                // Get the local table reference.
                tableName = mobileAppClient.getSyncTable('todoitem');

                // Sync local store to Azure table when app loads, or when login complete.
                syncContext.push().then((res) => {

                    // Push completed
                    // Pull items from the Azure table after syncing to Azure.
                    syncContext.pull(new WindowsAzure.Query('todoitem')).then((data) => {
                        alert(JSON.stringify(data));
                    }, (err) => {
                        alert(err);
                    });

                });

            }, function (err) {
                alert(err);
            });
        });

  }

}

【问题讨论】:

  • 你是通过网络浏览器还是物理设备测试的?
  • @AaronChen 我已经在安卓模拟器上测试过了。
  • 你使用的是js库还是cordova插件? github.com/Azure/azure-mobile-apps-cordova-client
  • @SurajRao 我添加了“azure-mobile-apps-client”节点模块和“cordova-plugin-ms-azure-mobile-apps”cordova 插件。还添加了“cordova-sqlite-storage”和“cordova-plugin-inappbrowser”等依赖插件。
  • 使用一个就足够了

标签: sqlite cordova ionic-framework ionic2 azure-mobile-services


【解决方案1】:

这可能是您的客户端数据库表列与您的远程数据库不匹配。请注意,通过迁移删除列并不会删除 SQLite 中的列。

【讨论】:

  • 我记得,如果我使用命令行创建项目。然后它工作正常。我只有在使用 VS 离子模板创建时才遇到这个问题。
猜你喜欢
  • 1970-01-01
  • 2020-05-26
  • 2020-11-06
  • 1970-01-01
  • 2012-07-30
  • 2013-01-08
  • 1970-01-01
  • 2021-06-25
  • 2020-01-21
相关资源
最近更新 更多