loopback-ds-computed-mixin
loopback-ds-computed-mixin copied to clipboard
MySQL connecter error
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.
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.
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)
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