【问题标题】:Loopback offline sync with cordova sqlite使用cordova s​​qlite环回离线同步
【发布时间】:2017-10-02 15:41:45
【问题描述】:

我正在使用 sqlite 作为其数据源创建一个带有环回离线同步的 cordova 应用程序。这是我的代码:

lbclient\datasources.json:

{
 "remote": {
    "connector": "remote"
},
 "sqlite": {
    "connector": "sqlite",
    "location": "default",
    "file_name": "sqlite.sqlite3",
    "debug": false
  }
}

lbclient\lbclient.js:

'use strict';

var loopback = require('loopback');
var boot = require('loopback-boot');
var client = module.exports = loopback();

if (client.connectorRegistry) {
   console.log(client.connectorRegistry);
}
client.connector('sqlite', require('loopback-connector-cordova-sqlite'));

boot(client);

在我的 browser.bundle.js 上,我注意到 try 块被执行了两次

app.dataSource = function(name, config) {
  try {
    var ds = dataSourcesFromConfig(name, config, this.connectors, this.registry);
    this.dataSources[name] =
    this.dataSources[classify(name)] =
    this.dataSources[camelize(name)] = ds;
    ds.app = this;
    return ds;
  } catch (err) {
    if (err.message) {
    err.message = g.f('Cannot create data source %s: %s',
    JSON.stringify(name), err.message);
  }
    throw err;
  }
};

这是 dataSourcesFromConfig:

function dataSourcesFromConfig(name, config, connectorRegistry, registry) {
  var connectorPath;

  assert(typeof config === 'object',
  'can not create data source without config object');

  if (typeof config.connector === 'string') {
     name = config.connector;
     if (connectorRegistry[name]) {
        config.connector = connectorRegistry[name];
     } else {
        connectorPath = path.join(__dirname, 'connectors', name + '.js');

        if (fs.existsSync(connectorPath)) {
          config.connector = require(connectorPath);
        }
     }
    if (config.connector && typeof config.connector === 'object' && !config.connector.name)
        config.connector.name = name;
    }

   return registry.createDataSource(config);
 }

在第一次运行 dataSourcesFromConfig 时,db (sqlite) 初始化得很好,但在第二次运行时,我现在收到错误:

browser.bundle.js:93172 Uncaught TypeError: Cannot create data source "sqlite": fs.existsSync is not a function

为什么我会遇到这种情况?你能帮我么。太感谢了。

【问题讨论】:

    标签: node.js sqlite cordova loopbackjs


    【解决方案1】:

    您必须要求连接器,否则 browserify 不知道它需要包含在最终包中。以编程方式进行,您可以内联 require sqlite 连接器,然后在要与 sqlite 一起使用的每个模型上使用 attachTo。以启动脚本为例:

    const DataSource = require("loopback-datasource.juggler").Datasource    
    
    module.exports = (app) => {
    
       const local = new Datasource({
            file_name:"dev.sqlite3",
            location: "default",
            name: "local",
            connector: require("loopback-connector-cordova-sqlite")
        })
    
        app.models["myModel"].attachTo(local)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2020-05-12
      • 2018-05-03
      • 2016-11-01
      • 2016-03-27
      • 2018-06-07
      相关资源
      最近更新 更多