totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

$.model is undefiend #727

Closed neophylon closed 5 years ago

neophylon commented 5 years ago

I have got a problem call $.model in schema. my souce is following . and I use postman url : http://localhost/api/v3/test method : post body : { "size" : 10, "page" :1, "type":"body", "section":"stand" } I can't figure out this problem.

controller/api.js

exports.install = function(){
    ROUTE('POST /api/v3/test',['authorize','*API --> @query']);
 }

schemas/api.js

const ObjectID = require('mongodb').ObjectID,
    uuidv4 = require('uuid/v4'),
    _ = require("lodash");

NEWSCHEMA('API').make(function(schema) {
    schema.define('id','String');
    schema.define('token','String');
    schema.define('type','String');
    schema.define('size','Number');
    schema.define('page','Number');
    schema.define('section','String');// work,stand/seat/lying || love....
    schema.setQuery(async function($){
       console.log('>>>>',$.model);
       console.log($.model['type');
    }

error message

>>>> undefined
======= 2019-08-21 11:01:02: TypeError: Cannot read property 'type' of undefined TypeError: Cannot read property 'type' of undefined
    at SchemaBuilderEntity.onQuery (/opt/WEBFLEX-API/schemas/api.js:77:27)
    at SchemaBuilderEntity.query (/opt/WEBFLEX-API/node_modules/total.js/builders.js:1514:9)
    at Controller.$query (/opt/WEBFLEX-API/node_modules/total.js/index.js:10410:19)
    at Controller.controller_json_workflow (/opt/WEBFLEX-API/node_modules/total.js/index.js:17256:17)
    at IncomingMessage.PROTO.$total_execute2 (/opt/WEBFLEX-API/node_modules/total.js/index.js:15111:32)
    at IncomingMessage.PROTO.$total_execute (/opt/WEBFLEX-API/node_modules/total.js/index.js:15079:9)
    at subscribe_validate_callback (/opt/WEBFLEX-API/node_modules/total.js/index.js:10176:6)
    at /opt/WEBFLEX-API/node_modules/total.js/index.js:15175:5
    at onSchema_callback (/opt/WEBFLEX-API/node_modules/total.js/index.js:5582:3)
    at SchemaBuilderEntity.make (/opt/WEBFLEX-API/node_modules/total.js/builders.js:1752:15)

==> I can't use $.model in schema.setQuery function, but $.body['type'] is work. in schema.setQuery console.log($.mode['type']) : undefined type console.log($.body['type']) : body

molda commented 5 years ago

$.model is only available in methods where it make sense, such as setInsert, setUpdate, setSave Also $.model is validated against the schema defined properties and in the setQuery you don't need that. In your case the data you send to your server is more like a filter and it has nothing to do with the model. So using $.body['type'] is the right way to go.

neophylon commented 5 years ago

Thank you advise.