Related
I have to use the data I receive, in particular I have to be able to use the data in 'properties':
I don't understand how to call them, instead of console.log
I tried to use an Object.values(), but so I can't access the 'properties' data that I have to reuse later.
const response = await notion.databases.query({
database_id: databaseId,
page_size: 2
});
var effort_data = []
effort_data.push({
response
})
effort_data.map(data => {
Object.values(data.response.results).map((data) => {
console.log('data', data)
})
})
this is my response :
data {
object: 'page',
id: '3b8e203e-79ae-4ddc-ba3a-e0f5b30f4d4e',
created_time: '2023-01-25T13:51:00.000Z',
last_edited_time: '2023-01-25T13:59:00.000Z',
created_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
last_edited_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
cover: null,
icon: { type: 'emoji', emoji: '👽' },
parent: {
type: 'database_id',
database_id: '1b02ff59-8cdb-47fb-a6f9-5904bcecbe72'
},
archived: false,
properties: {
Assignees: { id: '%24v1Q', type: 'people', people: [] },
Type: { id: '9dB%5E', type: 'select', select: [Object] },
Progetti: {
id: '%3AirU',
type: 'relation',
relation: [Array],
has_more: false
},
Epics: { id: 'B%7BSE', type: 'relation', relation: [], has_more: false },
'Creato da': { id: 'D%5Dzr', type: 'created_by', created_by: [Object] },
'Flussi coinvolti': { id: 'EFdR', type: 'relation', relation: [], has_more: false },
'Sprint corrente?': { id: 'G%5Cvg', type: 'formula', formula: [Object] },
Sprint: { id: 'Jz.%40', type: 'multi_select', multi_select: [] },
'🏃 Sprint': { id: 'O%3DHc', type: 'relation', relation: [], has_more: false },
Settore: { id: 'XYh%5D', type: 'select', select: null },
Timeline: { id: '_G%2Bl', type: 'date', date: [Object] },
Tasks: {
id: 'bXv%3F',
type: 'relation',
relation: [Array],
has_more: false
},
'Data creazione': {
id: 'e_r%7D',
type: 'created_time',
created_time: '2023-01-25T13:51:00.000Z'
},
'Epic status': { id: 'nRGT', type: 'rollup', rollup: [Object] },
'Ultimo aggiornamento da': { id: 'ogbh', type: 'last_edited_by', last_edited_by: [Object] },
'Data ultimo aggiornamento': {
id: 'p%5CYK',
type: 'last_edited_time',
last_edited_time: '2023-01-25T13:59:00.000Z'
},
Priority: { id: '%7BMEq', type: 'select', select: [Object] },
Status: { id: '%7CF4-', type: 'select', select: [Object] },
Name: { id: 'title', type: 'title', title: [Array] }
},
url: 'https://www.notion.so/Endpoint-per-ordini-card-da-esterni-3b8e203e79ae4ddcba3ae0f5b30f4d4e'
}
I have a schema, and using AJV to verify object. Now I can check required some field accords to other field value. But I want to check more than 1 field, how I can do it?
Schema:
{
type: 'object',
required: [
'notify',
],
additionalProperties: true,
properties: {
notify: { type: 'array' },
mode: { type: 'number', enum: [0, 1, 2, 3] }
},
if: {
properties: {
mode: {
enum: [1, 2],
},
},
},
then: {
required: ['temperature'],
},
else: {
if: {
properties: {
mode: {
const: 3,
},
},
},
then: {
required: ['temperature', 'humidity'],
},
},
}
this schema can check if mode: 1 or 2 then require temperature else if mode: 3 then require both temperature and humidity.
But I want to check notify length accords to mode. I tried to add if else in notify defincation but it do not work:
{
type: 'object',
required: [
'notify',
],
additionalProperties: true,
properties: {
notify: {
type: 'array',
if: {
properties: {
mode: {
enum: [1, 2],
},
},
},
then: {
minItems: 1,
},
else: {
minItems: 0,
},
},
mode: { type: 'number', enum: [0, 1, 2, 3] }
},
if: {
properties: {
mode: {
enum: [1, 2],
},
},
},
then: {
required: ['temperature'],
},
else: {
if: {
properties: {
mode: {
const: 3,
},
},
},
then: {
required: ['temperature', 'humidity'],
},
},
}
I have a fastify route method with the following schema.
fastify.post('/club', createClubSchema, createClub(fastify));
const createClubSchema = {
schema: {
tags: ['club'],
security: [
{
ApiKeyAuth: [],
},
],
body: {
type: 'object',
required: ['name', 'description'],
properties: {
name: { type: 'string', minLength: 3 },
description: { type: 'string', minLength: 3 },
logoUrl: { type: 'string', minLength: 3 },
},
},
response: {
200: {
type: 'object',
properties: {
status: { type: 'string' },
data: {
type: 'object',
properties: {
name: { type: 'string' },
description: { type: 'string' },
id: { type: 'number' },
color: { type: 'string' },
logoUrl: { type: 'string' },
createdAt: { type: 'string' },
updatedAt: { type: 'string' },
},
},
},
},
},
},
preHandler: [grantAccess('create', 'club')],
};
Now this route is in folder where there is autohooks.js is located that has a prehandler hook of it's own which checks whether the request has token in it for authentication purposes.
The problem is after the preHandler hook is called on both the area the createClub controller method is called, so a total of 2 times.
What is the issue? and how can i solve this?
EDIT
This is the autoHooks.js file
const authentication = require('../../middlewares/authentication');
module.exports = async function (fastify, opts, next) {
fastify.addHook('preHandler', authentication.authenticate(fastify));
};
I am following this toutorial: https://github.com/codeBelt/open-api-documentation/blob/master/src/openApiDocumentation.js
Can I validate my openApiDocumentation.js file somewhow? I get:
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
I am attaching mi .js file. Maybe you guys will see a typo here. Thanks in advance.
.js file:
const USER_TYPES = {
EXCHANGE: 'xxx',
GIVEAWAY: 'xxx'
}
const openApiDocumentation = {
openapi: '3.0.1',
info: {
version: '1.3.0',
title: 'xxx',
description: 'xxx',
contact: {
name: 'xxx',
email: 'xxx',
}
},
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
},
servers: [
{
url: 'http://localhost:4000/',
description: 'Local server',
},
],
tags: [
{
name: 'Books CRUD operations',
},
],
paths: {
'/findAllBooks': {
get: {
tags: ['CRUD operations'],
description: 'Get all Book offers',
operationId: 'getUsers',
parameters: [
{
name: 'page',
in: 'query',
schema: {
type: 'integer',
default: 1,
},
required: true,
description: 'Page numer used pagination.',
},
],
responses: {
'200': {
description: 'Books were obtained',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Books',
},
},
},
},
'500': {
description: 'Missing parameters',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
message: 'page qyery parameter is missing',
internal_code: 'missing_parameters',
},
},
},
},
},
},
},
},
components: {
schemas: {
coverImg: {
type: 'string',
example: 'http:',
},
image: {
type: 'string',
example: 'http',
},
category: {
type: 'string',
example: 'Crafts & Hobbies',
},
linkTypes: {
type: 'object',
properties: {
coverImg: {
$ref: '#/components/schemas/coverImg',
},
images: {
type: 'array',
items: {
$ref: '#/components/schemas/image',
},
}
}
},
offerID: {
type: 'string',
example: '27301927',
},
userID: {
type: 'string',
example: 'efdc5192',
},
title: {
type: 'string',
example: 'Quilting For Dummies',
},
description: {
type: 'string',
example: 'You ',
},
categories: {
type: 'array',
items: {
$ref: '#/components/schemas/category',
},
},
links: {
type: 'object',
items: {
$ref: '#/components/schemas/linkTypes',
},
},
offerType: {
type: 'string',
enum: USER_TYPES,
default: USER_TYPES.EXCHANGE,
},
Book: {
type: 'object',
properties: {
offerID: {
$ref: '#/components/schemas/offerID',
},
userID: {
$ref: '#/components/schemas/userID',
},
title: {
$ref: '#/components/schemas/title',
},
description: {
$ref: '#/components/schemas/description',
},
categories: {
$ref: '#/components/schemas/categories',
},
imageLinks: {
$ref: '#/components/schemas/links',
},
offerType: {
$ref: '#/components/schemas/offerType',
},
},
},
Books: {
type: 'object',
properties: {
users: {
type: 'array',
items: {
$ref: '#/components/schemas/Book',
},
},
},
},
Error: {
type: 'object',
properties: {
message: {
type: 'string',
},
internal_code: {
type: 'string',
},
},
},
},
},
};
There are few mistakes,
license must be inside the info object
info: {
version: '1.3.0',
title: 'xxx',
description: 'xxx',
contact: {
name: 'xxx',
email: 'xxx' // make sure you have used valid email address!
},
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
}
}
enum will allow only array not an object and here you have passed object USER_TYPES, corrected below:
const USER_TYPES = {
EXCHANGE: 'xxx',
GIVEAWAY: 'xxx'
};
const USER_TYPES_ENUM = [
USER_TYPES.EXCHANGE,
USER_TYPES.GIVEAWAY
];
offerType: {
type: 'string',
enum: USER_TYPES_ENUM,
default: USER_TYPES.EXCHANGE,
},
For best practice use https://editor.swagger.io/ (also they have provided a option to convert json to yaml under Edit > Convert to YAML)!
I try to create mongoose schema containing array of objects. Schema looks like:
const mapSchema = new Schema({
name: {
type: String
},
description: {
type: String
},
rate: {
type: Number,
default: 0
},
sumOfRates: {
type: Number,
default: 0
},
places: [
{
name: String,
description: String,
difficulty: { type: String, default: 0 },
sumOfRates: { type: String, default: 0 },
lat: Number,
lng: Number
}
]
}, {
timestamps: true
})
Places is array of objects. I tried to create mongoose schema and use it as element of places array(places: [ref: 'placeSchema']).
const placeSchema = new Schema({
name: {
type: String
},
description: {
type: String
},
difficulty: {
type: Number,
default: 0
},
lat: {
type: Number
},
lng: {
type: Number
}
})
But I think it would create another, separeted document in mongo(places document). Does it? I try to avoid such situation To keep it in one document.Now(code pasted above), gives me an error, when i try to insert json in postman:
{
"name": "ergergergg",
"description": "rgergrg",
"places": [
{
"name":"cvcv",
"description":"rgergrg",
"lat":233,
"lng":232
}]
}
Error:
ValidationError: places: Cast to Array failed for value "[object Object]" at path "places"
Why such error? How to fix this?
The appropriate way to go would be to use of a sub schema.
const subSchema = new Schema({
name: {
type: String,
},
description: {
type: String,
},
difficulty: {
type: Number,
default: 0,
},
lat: {
type: Number,
},
lng: {
type: Number,
},
});
const mapSchema = new Schema({
name: {
type: String,
},
description: {
type: String,
},
rate: {
type: Number,
default: 0,
},
sumOfRates: {
type: Number,
default: 0,
},
places: [subSchema],
}, {
timestamps: true
});
To insert a new data
mapSchema.insert({
name: "ergergergg",
description: "rgergrg",
places: [
{
name: "cvcv",
description: "rgergrg",
lat: 233,
lng: 232,
},
{
name: "22",
description: "erwer2",
lat: 123,
lng: 321,
},
],
});
To make the subSchema optional, you can maybe do something like this #DanMossa :
pages: {
type: [subSchema],
required: false
},