Closed mahdi-gh33 closed 7 years ago
Can I see your schema declaration?
const Fs = require('fs');
NEWSCHEMA('Service').make(function(schema) {
schema.define('id', 'String(40)');
schema.define('Title', 'String(30)');
schema.define('TimeStamp', Date);
schema.define('docType', 'String(40)');
});
I don't write any validation yet because I can't use schema for that response.
Yes, there is a problem. Because the schema is created from the whole response, not from rows
only.
You need to update your schema:
NEWSCHEMA('ServiceRow').make(function(schema) {
schema.define('id', 'String(40)');
schema.define('Title', 'String(30)');
schema.define('TimeStamp', Date);
schema.define('docType', 'String(40)');
});
NEWSCHEMA('Service').make(function(schema) {
schema.define('rows', '[ServiceRow]');
});
and now it should work:
builder.schema('service');
builder.exec(function (err, response) {
console.log(response.rows);
// each item should be ServiceRow
});
Actually I my real reponse is in value
property of each rows
.
Now the response is like this
[
{
"id": "Service_3a287fd1-f80a-4624-9862-28409681dae0",
"key": [
"service"
],
"value": {
"id": "Service_3a287fd1-f80a-4624-9862-28409681dae0",
"TimeStamp": "2017-09-26T14:06:54.894Z",
"Title": "NFC"
}
},
{
"id": "Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f",
"key": [
"service"
],
"value": {
"id": "Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f",
"TimeStamp": "2017-09-26T14:06:54.894Z",
"Title": "Wifi"
}
},
{
"id": "Service_5d98aea5-7065-42c6-855e-2069faa9ef93",
"key": [
"service"
],
"value": {
"id": "Service_5d98aea5-7065-42c6-855e-2069faa9ef93",
"TimeStamp": "2017-09-26T14:06:54.894Z",
"Title": "GPS"
}
}
]
But the value
is my real schema data,
So I change schema to :
`NEWSCHEMA('ServiceValue').make(function (schema) {
schema.define('id', 'String(40)');
schema.define('Title', 'String(30)');
schema.define('TimeStamp', Date);
schema.define('docType', 'String(40)');
});
NEWSCHEMA('ServiceRow').make(function (schema) {
schema.define('value', '[ServiceValue]');
});
NEWSCHEMA('Service').make(function (schema) {
schema.define('rows', '[ServiceRow]');
});`
and in ctrl:
builder.schema('service');
builder.exec(function (err, response) {
console.log(response.rows.value);
// each item should be ServiceRow
});
But it doesn't work.
Can you send me a small example on my email?
Sorry beacuse of this delay I'm working on my web service until I can send example.
No problem.
example-schema.zip Hi peter this is my example and I send you some information with email for you to use that.
Thank you
Something new?
I sent an email to you about my problem.
Sorry for delay. This is solution:
NEWSCHEMA('ServiceRowValue').make(function(schema) {
schema.define('id', 'String');
schema.define('TimeStamp', 'Date');
schema.define('Title', 'String');
});
NEWSCHEMA('ServiceRow').make(function (schema) {
schema.define('id', 'String(40)');
schema.define('key', '[String]');
schema.define('value', 'ServiceRowValue');
// Or just use Object:
// schema.define('value', 'Object');
schema.setSave(function($) {
console.log($.model);
$.callback(SUCCESS(true));
});
});
NEWSCHEMA('Service').make(function (schema) {
schema.define('rows', '[ServiceRow]');
});
Thank you it's working now . You are the best :-)
Hi @petersirka I have rest service with response like this :
{"total_rows":69,"rows":[ {"id":"Service_3a287fd1-f80a-4624-9862-28409681dae0","key":["service"],"value":{"id":"Service_3a287fd1-f80a-4624-9862-28409681dae0","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"NFC"}}, {"id":"Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f","key":["service"],"value":{"id":"Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"Wifi"}}, {"id":"Service_5d98aea5-7065-42c6-855e-2069faa9ef93","key":["service"],"value":{"id":"Service_5d98aea5-7065-42c6-855e-2069faa9ef93","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"GPS"}} ] }
The real response is in
value
property of response . I have a rest builder like this :` RESTBuilder.make(function (builder) { builder.url('http://example.com/_design/dev_customerId/_view/customerId? connection_timeout=60000&inclusive_end=true&key=%5B+%22' + customerId + '%22,+%22' + docType + '%22+%5D&limit=6&skip=0&stale=false');
I have a array of json model (I call it
service
) in value property of response which I should write a for loop for returning that but I can't use schema for that because if I uncomment the//builder.schema('service');
I have e error :Cannot read property 'length' of undefined
What should I do if I want to use schema features like validation for my rest builder that?Thank you.