Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
How to use on model into another model

I have two separated models name employee and user with some common data and a accountId to link each other. One record for each model is as below:

Employee
 _id:1639137618164

 name:"*****"
        
 sites:Array
 company:”Software”
 jobTitle: “Associate Software Engineer”
 email:"*****"
 phonesms:"1"
 phonevoice:"1"
 licenseNumber:""
 avatarStamp:0
 pined:false
 accountId: 1639129314021 //link attribute
 countryCodeSms:"US"
 countryCodeVoice:"US"
 inviteCode:"09611"
 emergencyRoles: Object
 invite: "booksRbUoSM"

and for User is

_id:1639129314021

firstName:"*** ***"

lastName:"****"
companyName:"SITS"
passwordReset:""
activated:true
accountId:1639129314021 // link attribute
siteId:-1
    
role:0
    
last:1639137913873
blocked:false
blockedtext:""
trialend:1640338913781
paytype:1
hashAccess:Array
emergencyEnabled:false
visitorTypes:Array
sitesLimit:2
manualCreation:false
username:"*************"
    
password:"**********************************"
    
email:"*********************"
passwordNotification:true
passwordLifecycle:1646906375986
isNewUser:true
listFeature:Object

I want to search from user model whether if any employee exists with that accountId or not, if there is employee associated with accountId, then add new employee with the available common data. otherwise not. How can i do this in loopback. I am stuck here!

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

I am using loopback version 2.x.x. It turns out to be very simple

currentModel.app.models.modelYouWantToAccess 
about 4 years ago · Santiago Gelvez Relatório

0

Try with below

var  employee_data = Employee  .filter(x => User.map(y => y.accountId).includes(x.accountId));
about 4 years ago · Santiago Gelvez Relatório

0

In Loopback 4

Solution 1:

In your user controller where you have the access to the UserRepository

Assuming you know the accountId and the accountId only exists if you have an employee record you can have a method

@get('/users/{accountId})
async findUserWhoIsAnEmployee(@param.path.string('accountId') accountId: string){
 return this.userRepository.findOne({where: {accountId}})
}

Assuming you don't know the accountId...

You can access the employee repository in your UserController by passing it as an argument in the constructor function and finding the user, then checking in the Employee record. (Not optimal since two queries will be made on your DB)

export class UserController{
  constructor(
     @repository(UserRepository) public userRepository: UserRepository,
     @repository(EmployeeRepository) private employeeRepository: EmployeeRepository
  )
  // returns employee record or null if none
  @get('/users/{id}/employee-record)
  async findUserWhoIsAnEmployee(@param.path.string('id') id: string){
   const user = await this.userRepository.findById(id);
   return (!user) ? null : this.employeeRepository.findOne({where: {accountId: user.accountId}})
  }
}

Solution 2:

Where you have a relation between the User and Employee Model (hasOne, preferrably)

You can find that employee record on the user

// returns employee record of a user if exists 
@get('/users/{id})
async findUserWhoIsAnEmployee(@param.path.string('id') id: string){
 return this.userRepository.findById(id).employeeRecord()
}
//Where employeeRecord is a reference property on the user Model.
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda