Good evening,
I just started with loopback (literally), so I went for the commands explained in the documentation:
Started the loopback project with the command:
slc loopback
Generated a model with the loopback model generator with the command:
slc loopback:model
When the generator starts, it asks for model name, storing method, base class, plural, whether we want it to be a common model or a server model. After that it asks for the properties of the model.
I have a model that could be like this:
MODEL NAME
Property1 : String,
Property2 : String,
Property3 : Number,
Property4 : {
obj.Property1 : String,
obj.Property2 : String,
obj.Property3 : String
},
Property5 : String
I thought that by choosing "Object" as the type of property it would ask me for additional properties on that object, but it didn't. Now I have no idea how to create those additional properties that are inside the object of this model.
How would I go about creating the properties that are nested inside the Property4 Object? Am I missing something from the loopback:model generator?
Well slc loopback:model doesn't do that. You just have to specify yourself in the generated json file(possibly in common/models/ directory) in the properties object:
"properties": {
...
"Property4": {
"type": {
"Property1" : "String",
"Property2" : "String",
"Property3" : "String"
}
},
...
}
If you want any of the properties to be required you can do it like this:
"properties": {
...
"Property4": {
"type": {
"Property1" : {
"type": "String"
"required": true
},
"Property2" : "String",
"Property3" : "String"
}
},
...
}
If the object doesn't have a "type" property, you can just do this:
"properties": {
...
"Property4": {
"Property1" : "String",
"Property2" : "String",
"Property3" : "String"
},
...
}
You can also put this definition into a another model and reference that here:
"properties": {
...
"Property4": "AnotherModel",
...
}
I suggest you read through the 'properties' section of this document and also 'object types' section of this document.
Related
i want to convert field 'districId' that has long data-type to keyword/text for wildcard search. please guid me how can convert data-type from long to keyword/text data-type in elasticsearch
PUT geoxingsite/_mapping
{
"properties": {
"districtId": {
"type": "keyword"
}
}
}
i am getting error below...
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "mapper [districtId] cannot be changed from type [long] to [keyword]"
}
],
"type" : "illegal_argument_exception",
"reason" : "mapper [districtId] cannot be changed from type [long] to [keyword]"
},
"status" : 400
}
You cannot change the type of a field once it's created. However, you can add a sub-field like this:
PUT geoxingsite/_mapping
{
"properties": {
"districtId": {
"type": "long",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
When done you need to update your index in place using
POST geoxingsite/_update_by_query?wait_for_completion=false
When the task has run, you'll have a new field called districtId.keyword on which you can run your wildcard queries
I am trying to push arrays from one collection to another.
this is the code I used in my server js
updateSettings: function(generalValue) {
let userId = Meteor.userId();
let settingsDetails = GeneralSettings.findOne({
"_id": generalValue
});
Meteor.users.update({
_id: userId
}, {
$push: {
"settings._id": generalValue,
"settings.name": settingsDetails.name,
"settings.description": settingsDetails.description,
"settings.tag": settingsDetails.tag,
"settings.type": settingsDetails.type,
"settings.status": settingsDetails.status
}
})
}
updateSettings is the meteor method. GeneralSettings is the first collection and user is the second collection. I want to push arrays from GeneralSettings to users collection. While I try this the result i got is like
"settings" : {
"_id" : [
"GdfaHPoT5FXW78aw5",
"GdfaHPoT5FXW78awi"
],
"name" : [
"URL",
"TEXT"
],
"description" : [
"https://docs.mongodb.org/manual/reference/method/db.collection.update/",
"this is a random text"
],
"tag" : [
"This is a demo of an Url selected",
"demo for random text"
],
"type" : [
"url",
"text"
],
"status" : [
false,
false
]
}
But the output I want is
"settings" : {
"_id" : "GdfaHPoT5FXW78aw5",
"name" : "URL",
"description" :
"https://docs.mongodb.org/manual/reference/method/db.collection.update/",
"tag" :"This is a demo of an Url selected",
"type" : "url",
"status" : false
},
What changes to be made in my server.js inorder to get this output
This is one case where you "do not want" to use "dot notation". The $push operator expects and Object or is basically going to add the "right side" to the array named in the "left side key":
// Make your life easy and just update this object
settingDetails._id = generalValue;
// Then you are just "pushing" the whole thing into the "settings" array
Meteor.users.update(
{ _id: userId },
{ "$push": { "settings": settingDetails } }
)
When you used "dot notation" on each item, then that is asking to create "arrays" for "each" of the individual "keys" provided. So it's just "one" array key, and the object to add as the argument.
I am using swagger doc to generate API for my app. Doc is written in yml and in some place it is going to define some props which are enum (we use mysql). This is looks like:
properties:
type:
enum:
- "first"
- "second"
- "example"
type: "string"
title:
type: "string"
... // And so on and on
I am expecting to get something like this:
{
"type": "string",
"title": "string",
}
As you can see, type field is going to be a string as was defined in config file, but I am receiving this one instead:
{
"type": "first",
"title": "string",
}
Swagger sets first value as a data type and it is absolutely incorrect.
So the question is how to get value "string" for "type" field.
I have been writing my swagger documentation in JSON. Below is an example of how I have my "enums" set up. Maybe you can translate it to YML to get a better idea.
"parameters":[
{
"name":"userID",
"in":"path",
"description":"TO BE DETERMINED.",
"required":true,
"type":"string"
},
{
"name":"embedded",
"in":"query",
"description":"TO BE DETERMINED",
"required":false,
"type":"array",
"items":{
"enum":[
"address",
"state"
]
},
"collectionFormat":"csv"
}
If you look at items you see that I don't use a type and it works fine.
I am trying to parse this simple json string:
var dataJSON = {};
var data;
dataJSON = {
"status": "OK",
"messages" : [{
"user" : {
"id" : "4",
"status" : "offline",
"name" : "dummy",
"pictures" : ["pic.jpg"]
},
"message" : "Hey",
"timestamp" : 1395660658
}, {
"user" : {
"id" : "2",
"status" : "online",
"name" : "dummy1",
"pictures" : ["pic1.jpg"]
},
"message" : "hello",
"timestamp" : 1395660658
}]
};
console.log('test');
console.log(dataJSON);
//parse data
data = JSON.parse(dataJSON);
but I am getting the following error:
"unable to parse json string"
I have mo idea why, cheers.
You don't have to parse it at all; it's a JavaScript object already.
The acronym "JSON" stands for JavaScript Object Notation. It's a restricted form of the native syntax in JavaScript for creating objects "on the fly". To say that another way, JavaScript's native object literal syntax is a superset of JSON. What you've typed in there, as the value of your "dataJSON" variable, is a JavaScript object literal expression. The value of such an expression is a reference to an object. No parsing necessary, since the JavaScript parser itself has already done so.
edit — if you really do need a JSON string for testing purposes, then I think the easiest way to do that is to use JSON.stringify() to convert an object into a string, and then pass it into the test code. In your example, that'd look like:
dataJSON = JSON.stringify({
"status": "OK",
"messages" : [{
"user" : {
"id" : "4",
"status" : "offline",
"name" : "dummy",
"pictures" : ["pic.jpg"]
},
"message" : "Hey",
"timestamp" : 1395660658
}, {
"user" : {
"id" : "2",
"status" : "online",
"name" : "dummy1",
"pictures" : ["pic1.jpg"]
},
"message" : "hello",
"timestamp" : 1395660658
}]
});
It's a little easier than trying to construct the string by hand because of the "quote quoting" nuisance. Of course the object you pass in should be one that actually can be represented as JSON, but your sample above is definitely OK.
I've been writing simple JSON schemas but I ran into an API input call that is a bit more complex. I have one restful end route that can take 3 very different types of JSON:
localhost/foo
can take:
{ "type" : "ice_cream", "cone" : "waffle" ...}
or
{"type" : "hot_dog", "bun" : "wheat" ...}
If the "type" key contains "ice_cream", I only ever want to see the key "cone" and not the key "bun". Similiarly if "type" contains "hot_dog" I only want to see "bun" and not "cone". I know I can pattern match to make sure I only ever see type "ice_cream" or type "hot_dog", but I don't know how to force the requirement of certain other fields if that key is set to that value. I see that there is a json schema field called "dependency" but I haven't found any good examples on how to use it.
BTW, I'm not sure if this input JSON is good form (overloading the type of JSON structure it takes, effectively), but I don't have the option of changing the api.
I finally got some information about this - it turns out you can make a union of several different objects that are valid like so:
{
"description" : "Food",
"type" : [
{
"type" : "object",
"additionalProperties" : false,
"properties" : {
"type" : {
"type" : "string",
"required" : true,
"enum": [
"hot_dog"
]
},
"bun" : {
"type" : "string",
"required" : true
},
"ketchup" : {
"type" : "string",
"required" : true
}
}
},
{
"type" : "object",
"additionalProperties" : false,
"properties" : {
"type" : {
"type" : "string",
"required" : true,
"enum": [
"ice_cream"
]
},
"cone" : {
"type" : "string",
"required" : true
},
"chocolate_sauce" : {
"type" : "string",
"required" : true
}
}
}
]
}
I'm still not sure if this is valid JSON, since my Schemavalidator dies on some invalid input, but it accepts the valid input as expected.