loopback-example-offline-sync icon indicating copy to clipboard operation
loopback-example-offline-sync copied to clipboard

using mysql as a datasource

Open juliuscarvajal opened this issue 11 years ago • 8 comments

Hi guys, I am using mysql as my datasource. I have added my models accordingly and I get my models in my controller.

Did a console log of one of the models and I get this: Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}

Doing the code below will result to TypeError: Cannot read property 'then' of undefined

Channel.find().$promise.then(function (result) {
  console.log(result);
});

What seems to be the problem? Why I am not getting my data from the model?

Other observations: Using the generated loopback-angular-ui-router, I was able to get my data. Doing a console log of one of the models resulted to this: function v(a){f(a||{},this)}

Whereas in this example, doing a consol log of one of the models resulted to this: ModelClass = function ModelConstructor(data, options) {

juliuscarvajal avatar Sep 15 '15 07:09 juliuscarvajal

You should not use $promise, that's a concept from ngResource, which we are not using here.

Try this code instead:

Channel.find().then(function (result) {
  console.log(result);
});

in this example, doing a consol log of one of the models resulted to this:

ModelClass = function ModelConstructor(data, options)

This is expected. What you see is a constructor method of LoopBack models as generated by loopback-datasource-juggler.

bajtos avatar Sep 21 '15 16:09 bajtos

Thanks, mate. I tried that code as well. I get an empty array for the objects from mysql. The Todo object works just fine.

It would be good if there is a working example of Loopback replication with pub sub using a MySQL db.

Below is the relevant code for your reference. Thanks mate.

angular.module('loopbackExampleFullStackApp')
  .controller('appCtrl', function appCtrl($scope, Location, Channel, Todo, $timeout) {

    Todo.find().then(function (result) {
      console.log(result);
    });

    Channel.find().then(function (result) {
      console.log(result);
      $scope.Channels = result;
    });

    Location.find().then(function (result) {
      console.log(result);
      $scope.Locations = result;
    });
  });

lbclient.js

angular.module('loopbackExampleFullStackApp')
  .value('Todo', client.models.LocalTodo)
  .value('RemoteTodo', client.models.RemoteTodo)
  .value('sync', client.sync)
  .value('network', client.network)
  .value('getReadableModelId', client.getReadableModelId)
  .value('Location', client.models.LocalLocation)
  .value('RemoteLocation', client.models.RemoteLocation)
  .value('Channel', client.models.LocalChannel)
  .value('RemoteChannel', client.models.RemoteChannel);

remote-location.json

{
  "name": "RemoteLocation",
  "base": "Location",
  "plural": "Locations",
  "trackChanges": false,
  "enableRemoteReplication": true
}

local-location.json

{
  "name": "LocalLocation",
  "base": "Location"
}

common/models/location.json

{
  "name": "Location",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "theschema",
    "table": "location"
  },
  "properties": {
    "id": {
      "type": "Number",
      "id": true,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "colourId": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "colour_id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "code": {
      "type": "String",
      "required": false,
      "length": 12,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "code",
        "dataType": "varchar",
        "dataLength": 12,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "name": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "name",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "location": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "location",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "timezone": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "timezone",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "data": {
      "type": "String",
      "required": false,
      "length": 65535,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "data",
        "dataType": "text",
        "dataLength": 65535,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

juliuscarvajal avatar Sep 22 '15 00:09 juliuscarvajal

+1

tenzan avatar Sep 22 '15 01:09 tenzan

Hi @juliuscarvajal, I am afraid I'm rather short of time. Could you please create a GitHub repository that I can clone and run locally to reproduce the problem you are facing? If the app requires any tables/data to exist in the MySQL database, then please include migration/setup script too.

bajtos avatar Sep 28 '15 16:09 bajtos

Hi @juliuscarvajal,

Could you please share a repo or alternatively reproduce the issue using loopback-sandbox and share it with us? (Please see: https://github.com/strongloop/loopback-sandbox)

Thanks!

Amir-61 avatar Nov 19 '15 00:11 Amir-61

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 23 '17 14:08 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Sep 06 '17 14:09 stale[bot]

Oh, this is a shame, I'm interested in this as well: >>It would be good if there is a working example of Loopback replication with pub sub using a MySQL db<<

jgwinner avatar Sep 14 '19 03:09 jgwinner