Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

currentModel.app.models.modelYouWantToAccess 
over 4 years ago · Santiago Trujillo Report

0

Try with below

var  employee_data = Employee  .filter(x => User.map(y => y.accountId).includes(x.accountId));
over 4 years ago · Santiago Trujillo Report

0

I suppose you are using mongoose and you have this part in your User model:

accountId: {
  type: mongoose.Schema.ObjectId,
  ref: 'Employee',
  required: [true, 'this field must be provided']
},

You can add a populate method to your schema so that related model's data gets added to each time you request data with User.find(), findOne, and all other methods starting with 'find'

userSchema.pre(/^find/, function(next) {
  this.populate({
    path: 'accountId',
    options: { // you can populate only some fields if you want
      select: 'name company jobTitle'
    }
  })

  next()
})

Then, you can use your User model as always, but now related Employee data gets added:

const given_id = "65465458qw7d7q1q7w1x" 
const user = await User.findOne({accountId: given_id})

// Note: you can use User.find if there can be
// more than one user with that accountId.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!