I have a loopback model as
{
"name": "myModel",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"description": "Channel name"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Now this is connected to a mongodb collection.This collection can have custom id in numeric like 1234 or Mongodb ObjectId when created using loopback.
Now when I query using GET
http://localhost:3000/api/myModel/1234.It is not able to get the object of that id.
I get an error message as
{"status":404,"statusCode":404,"code":"MODEL_NOT_FOUND","message": "Unknown \"myModel\" id \"1234\"}
If my id is alphanumeric like abcd1234 then I dont get this error.Is there a way in loopback to be able to process a numeric id.
The only reference I have is this that suggests to use alphanumeric ids.
Related
My database is in PostgreSQL.My model defination is as below -
{
"name": "City",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
I am using code below to auto update the tables but when I go to database after running the code below and I check fields, I see id field created with an auto increment. How can I prevent creation of "id" field ?
ds.autoupdate("City", function (er) {
if (er) throw err
});
How can I prevent creation of "id" field?
If you're using a database, you can't. See this issue.
A model without any id properties can only be used without attaching to a database.
Making the name property an Id looks like the only way to use it with a relational database.
"name": {
"type": "string",
"id": 1
}
I have been stuck with LoopBack for the last couple of hours trying to extend the User model with a custom model called Client.
{
"name": "Client",
"base": "User",
"plural": "Clients",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"schema": "LOOPBACK",
"table": "my_table_name"
}
},
"properties": {
"test": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
After doing the autoupdate I am not seeing any other properties in the client table than "test".
From the explorer I try to submit a new user, but I get the following error:
"uniqueness.Error: ER_BAD_FIELD_ERROR: Unknown column 'realm' in 'field list'"
Read every possible answer on google, but couldn't find a solution.
Try to manually create a column called 'realm' on your table 'my_table_name' and check if it fixes.
You need to exclude properties present in User but absent in your Model. My extended user has this property right before "properties" property:
"excludeBaseProperties": [
"realm",
"password",
"emailVerified",
"verificationToken"
],
if I have a usermodel and I define:
"events": {
"type": [
"Object"
]
},
Do I need to define anything else in the usermodel.js to be able to post things like: [{name: 'sample', ...}, ...] to the user table's events column?
I ask because if I remove this particular definition from the .json the app compiles and the database migrates, but with it in, the app compiles but the database states there was an issue with the users findByid. My debugging has narrowed it down to this particular set of code.
I think you can simply use this structure
{
"events":{
"type": [
{
"key": "type",
"key2": "type"
}
]
}
}
You can see a .js example here and .json example here.
but I can also see an issue with the implementation here that says
this model has problem. When we fetch the data with any get call, it
renders this particular field as ["Object Object"] even though the
data is properly saved in the database.
which I would recommend you to try on your own as it will depend a lot on versions and drivers.
Though I would like to ask that what kind of database are you using?
Another way would be to define the object you want in the array as a model, then use that model as your type:
Model: Class
{
"name": "Class",
"base": "Model",
"strict": true,
"idInjection": false,
"properties": {
"label": {
"type": "string",
"required": true
}
}
}
Model: Student
{
"name": "Student",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
"classes": {
"type": ["Class"],
"required": false
},
}
}
How to ensure uniqueness of a combination of multiple fields in loopback model. Like below is the model Organisation, I have two field name and contact in it, I want the combination of these two fields to be unique in the database.
For example :- while creating an organisation two records can have same value in the 'name' field but the combination of the value of the 'name' and the 'contact' field should be unique for each record in order to create it.
`{
"name": "Organisation",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"contact": {
"type": "number",
"required": true
}
}`
You can use indexes for this purpose. Check documentation, there are several very good examples that covers this topic.
"nameContactUniqueIndex": {
"keys": {
"name": 1,
"contact": -1
},
"options": {
"unique": true
}
}
A key value of 1 specifies ascending order, and -1 specifies descending order.
I have a strongloop app in which I am trying to post some data to my REST APIs and then trying to retrieve it from another user but I am not getting the "id" of that record to the user not owning the object. How do I access the generated "id" from the user not owning the object. There are no ACLs in my model file
I am expecting the generated unique user identifiers to be returned along with the json response, for example if I make a request like this -
http://localhost:80/api/tuitions?access_token=qvpvtsbWOLZYydUDcbO0VP6YrlfHhbURHpkvPjnUyU9wRHpclAyJh44RKJ6VU36x
the output is -
[
{
"area": [
"901"
],
"city": "9",
"class": "9",
"subjects": [
"902"
],
"userId": "5696915dec62196433db7950",
"created": "2016-01-14T18:40:07.761Z",
"updated": "2016-01-14T18:40:07.761Z",
"identifier": "5697eb8767e585347755c23b"
}
]
in my loopback explorer and my loopback tuition model file defines identifier as -
{
"name": "tuition",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
// ...
"identifier": {
"type": "string",
"id": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
But when I make the same request with my Angular App in my controller with something like -
Tuition.find({
filter: {
order: 'created DESC'
}
}).$promise;
I get this -
[{"area":["901"],"city":"9","class":"9","subjects":["902"],"userId":"5696915dec62196433db7950","created":"2016-01-14T18:40:07.761Z","updated":"2016-01-14T18:40:07.761Z"}]
which does not have identifier