It is the first time that I use this version (4) for development and I have a problem with loopback and mongodb indexing.
Of the two ids that are inside the db loopback it does not collect any.
It's a problem of the API or DB?
Model [Loopback]
import { Entity, model, property } from '#loopback/repository';
#model()
export class Ad extends Entity {
#property({
type: 'number',
id: true,
required: true,
})
id: number;
<...>
constructor(data?: Partial<Ad>) {
super(data);
}
}
Data on Mongo:
{
"_id": {
"$oid": "5c0e9c7730146d2448746834"
},
"id": 110722,
"creation_date": 1492075600000,
"update_date": 1492075921000,
...
}
Response on loopback GET /ads
[{
"id": null,
"creation_date": 1492075600000,
"update_date": 1492075921000,
...
},...]
Hello from the LoopBack team :)
I don't see any obvious problem in the code snippets you posted. What happens when you change id's type from number to string? Will it fix the problem?
Most likely, you have found a bug in LoopBack 4. Please report it via GitHub: https://github.com/strongloop/loopback-next/issues
I fixed the same issue by changing the id=String. Mongodb id contains both string and number. So, if you change the type of id=string (Model) the issue will be fixed.
Related
In loopback framework, is there a way to avoid updates for few fields
Below code allows updates for all fields that is passed in the API request body.
async updateById(
#param.path.number('id') id: number,
#requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Todo, {partial: true}),
},
},
})
todo: Todo,
): Promise<void> {
await this.todoRepository.updateById(id, todo);
}
As far as I understand from your question, you want to update some part of the object in the database.
this.repo.updateById(id,objectYouWantToUpdate)
This is going to work perfectly, just send the data you want to update and not the whole object.
exclude key can help
schema: getModelSchemaRef(Todo, {partial: true, exclude: ['title']})
I've just started with Loopback for the first time and I've started with LB4, their newest release. I'm looking to create a model with nested objects and arrays as per my JSON schema to which I followed the documentation which allowed me to create the base values of my schema, but I need to create the fields inside the objects and arrays, but I can't find the documentation or articles to help me understand this...
This is my JSON schema I'm trying to create a LB4 model with:
"socialProfiles": {
"facebook": {
"linked": 1,
"pullData": 1,
"linkID": 4434343,
"profile": "https://www.facebook.com/FBURL",
"registered": {
"date": "2018-05-04T12:41:27.838Z",
"verified": "2018-05-04T12:41:27.838Z",
"by": {
"id": 1,
"user": "USER"
}
}
},
}
Using the LB4 documentation, I can create my main field socialProfiles but I can't find where I go to create my fields inside this object... Here's my LB4 model code
import {Entity, model, property} from '#loopback/repository';
#model()
export class Users extends Entity {
#property({
type: 'object',
})
socialProfiles?: object;
constructor(data?: Partial<Users>) {
super(data)
}
}
How to do this?
If you want to store the object in the model itself (not with a relation), you can create an interface with something like:
export interface ISocialProfile {
"linked": number,
"pullData": number,
"linkID": number,
"profile": string,
"registered": {
"date": Timestamp,
"verified": Timestamp,
"by": {
"id": number,
"user": string
}
}
}
and then in your model, you can just add the type:
socialProfiles?: {[name: string]: ISocialProfile};
On a Category model I created, I have the following relation:
"subscriptions":
{
"type": "hasMany",
"model": "Subscription"
"foreignKey": "",
"options": { "nestRemoting": true } } }
How could I get the Count() result when running:
this.userService.getCategories(this.currentUser.id,
{include: {relation: 'subscriptions', scope: {type: 'count'}}})
.subscribe((data: any[]) => { this.categories = data };
I would like to count the number of subscription when getting the categories belonging to the user, in the same observable().
Like show above, I tried with the scope of type 'count'. nothing comes.
Thanks for any help.
As an answer:
Loopback provides the 'include' filter to be able to retrieve related models.
As a consequence an Observable is retrieved. It does contain the relationship.
Getting the Count() is as simple as writing {{category.subscriptions.length}}in the HTML.
Hope this helps.
I have below json and I want to add multiple has many relationship.
{
"Id": "2311",
"package": [
{
"0": "233123"
},
{
"1": "1987797"
}
]
}
I want something like this in my model.
package : hasMany('package'),
package : hasMany('name'),
How can I use the hasMany relationship on same attribute for 2 models ?
I'm not sure I understand your question.
When you define hasMany relationship, for example, a client hasMany packages, you get the following representation in your database:
Client 1 { // Client model hasMany relationship to Package model
name: "client1"
id: "1234"
}
Package 1 {
name: "package1"
id: "233123"
client: "1234" // this package belongs to client 1
}
Package 2 {
name: "package2"
id: "1987797"
client: "1234" // this package also belongs to client 1
}
Are you saying you want your Client model to hold 2 hasMany relationships, one for the Package name and one for the Package ID?
Please provide a bit more detail and I'll help you answer the question.
I have JSON coming from the server which looks like:
data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}
authentication-token: {
token: "token",
id: "id"
}
}
The idea is to store this two models (user, authentication-token) in ember store under the same names. When I gat the above mentioned response from a server, model user is saved successfully, but model authentication-token does not get saved to the store at all. When I log the data (in the adapter) before the data is passed to serializer I see that JSON has a structure which Ember-Data expects. I don't know whether the problem is that Ember-Data cannot handle two models in success at one time, and then save it to the corresponding models, or something else. Ideas?
Now it all makes sense to me. Of course, this was the problem in your last question. Anyway, ember-data's RESTAdapter can't handle this. If you're requesting a singular resource user it expects at most this user as a singular answer. Any other resource that may be "side-loaded" has to be an array. The requested singular user can either be a record under the user key or the first entry in an array unter the users key. Everything else (except meta data) has to be plural.
In case you're requesting a plural resource users, the main response must be under the users key, any sideloaded users that aren't part of the response prfixed with _ under the _users key.
For your example all this means that your json must be formatted like this:
data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}
authentication-tokens: [{
token: "token",
id: "id"
}]
}
If you can't change your server, you'd have to configure the RESTAdapter to normalize the JSON data through normalize of the Serializer.