Closed LandyCuadra closed 3 years ago
Hi @LandyCuadra, you can clean it via $clean()
method:
var data = model.$clean();
BTW: I recommend using Total.js v4 (the model is always clean in v4).
but I do not have a model var... look at how I do it.
this is my controller function
async function addClient() {
const self = this,
{body} = self,
data = body;
$WORKFLOW('InsuranceVerification','saveClient', data, (err, response) => {
if(err){
self.status = 500;
self.json(err);
return;
}
self.status = 200;
self.json(response);
}, self);
}
and this is in my schema
NEWSCHEMA('InsuranceVerification', (schema) => {
schema.define("clientId", String, true);
schema.define("patientId", String);
schema.define("apptDate", String);
schema.required("patientId", (model, op) => {
console.log("Operation", op);
return !!op.update
});
schema.required("apptDate", (model, op) => {
console.log("Operation", op);
return !!op.update
});
schema.addWorkflow('saveClient', async function ($) {
const model = {...$.clean(), ...schema.clean($.options)};
//OPERATION
$.success("client inserted successfuly");
});
}
I have tried to call it like this, and getting the model only with $.clean(), so it respect the schema.define but in the schema.required() the op comes empty and loss the controller (because I don't send it)
function addClient() {
const self = this;
const body = self.body;
body.method = self.res.req.method;
GETSCHEMA("InsuranceVerification").make(body, function (err, model) {
if (err) {
self.status = 500;
self.json(err);
return;
}
model.$workflow("saveClient", function (errSave, res) {
if (errSave) {
self.status = 500;
self.json(err);
return;
}
self.status = 200;
self.json(res);
});
});
}
Try to add: model.$controler(self)
and let me kwow. This can solve a problem with the op comes empty and loss the controller (because I don't send it)
function addClient() {
const self = this;
const body = self.body;
body.method = self.res.req.method;
GETSCHEMA("InsuranceVerification").make(body, function (err, model) {
if (err) {
self.status = 500;
self.json(err);
return;
}
// TRY TO ADD:
model.$controller(self);
model.$workflow("saveClient", function (errSave, res) {
if (errSave) {
self.status = 500;
self.json(err);
return;
}
self.status = 200;
self.json(res);
});
});
}
now I am getting the controller, but op in schema.required() still an empty object
I understand and this is a bit problem because the op
is assigned automatically. In your case you need to assign op
manually in the .make()
method (but it's not good way). I don't know why don't you use routing to the schemas.
This solves all problems:
ROUTE('POST /api/clients/add/ *InsuranceVerification --> @saveClient');
yeah, already tried it this way and it works, but the company wants the route poiting to a function.... thank you for your help!
In your case you need to assign
op
manually in the.make()
method (but it's not good way)
hi again, can show where I can find how to do this?, please, I checked the documentation but the only reference to operations for schema instances was to invoke an addOperation method... with this I could close the issue
yeah, already tried it this way and it works, but the company wants the route poiting to a function.... thank you for your help!
Total.js creates a function in the background. Routing with the schemas brings clear structure.
// This property is assigned to the `.required()` evaluator.
model.$$workflow = { update: 1 };
model.$controller(self);
model.$workflow("saveClient", function (errSave, res) {
});
Look to the $ACTION()
method: https://docs.totaljs.com/latest/en.html#api~global~%24ACTION
I am calling workflow from the controller with $WORKFLOW and sending an Object with the body received but can't get it clean, following the schema definition... any Idea of how to achieved that?