loopback-ds-computed-mixin icon indicating copy to clipboard operation
loopback-ds-computed-mixin copied to clipboard

MySQL connecter error

Open maxfi opened this issue 9 years ago • 3 comments

I tried to use this mixin and can't get it to work with the following setup:

server/server.js

var loopback = require('loopback');
var boot = require('loopback-boot');

// Otherwise Promise.map doesn't work.
global.Promise = require('bluebird');

var app = module.exports = loopback();

require('loopback-ds-computed-mixin')(app);

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    console.log('Web server listening at: %s', app.get('url'));
  });
};

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

common/models/myModel.json

{
  "name": "MyModel",
  "options": {
    "idInjection": false,
    "mysql": {
      "schema": "someSchema",
      "table": "myModel"
    }
  },
  "properties": {
    "description": "String"
  },
  "mixins": {
    "Computed": {
      "properties": {
        "description": "computedDescription"
      }
    }
  }
}

common/models/myModel.js

module.exports = function(MyModel) {
  MyModel.computedDescription = function(myModel) {
    return "some description";
  };
};

When I try to GET /api/MyModels I get an error from the mysql connector saying it can't find the description field.

maxfi avatar Sep 15 '15 09:09 maxfi

I haven't tried this with the MySQL connector. It might help if you add the description field to the table if it's not there yet.

beeman avatar Sep 15 '15 22:09 beeman

I have the same issue. The solution of adding the column to the table doesn't make sense since its a computed property (ie. it shouldnt be a real column)

stringbeans avatar Oct 06 '16 22:10 stringbeans

Sorry to be late. I faced this issue recently and solved it by setting global "scope" in the model JSON definition like this:

"scope": { "fields": { "description": false } }

I hope this can still help someone

Akeri avatar Dec 02 '17 10:12 Akeri