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

192
Views
Mongoose controller cant find a method from Repository

I currently have my code setup in 3 files: Search.model.js, Search.repository.js, and search.controller.js.

The issue is when I send a request to the Route it reaches the controller but cannot call the function in the Repository. The code is below:

search.repository.js

const mongoose = require('mongoose');
const Search = require('../models/search.model');
const perPage = config.PAGINATION_PERPAGE;

const searchRepository = {
    save: async (data) => {
        console.log("Reached save in Repository");
        try {
            let record = await Search.create(data);
            if (!record) {
                return null;
            }
            return record;
        } catch (e) {
            throw e;
        }
    },
};
module.exports = searchRepository;

search.controller.js

const mongoose = require("mongoose");
const searchRepo = "../repositories/search.repository";

class SearchController  {
constructor() {}

async store(req) {
  console.log("Reached Store");
    try{
      let record = await searchRepo.save(req.body);
      if (record) {
        return { status: 200, data: {}, message: "Saved Successfully" };
      } else {
        return { status: 201, data: {}, message: "Unable to save your record. Please try again later." };
      }
    }
    catch(e){
      return { status: 201, data: {}, message: e.message };
    }
}
}
module.exports = new SearchController();

The error I get:

{
    "status": 201,
    "data": {},
    "message": "searchRepo.save is not a function"
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The searchRepo variable in search.controller.js is not set to the searchRepository object found in search.repository.js. Make sure to export and import that object properly. It is currently only set to a string. Instead, you could use require (ex. require({file_path}).{identifier} to import it.

about 4 years ago · Juan Pablo Isaza Report

0

You are not importing searchRepo properly in search.controller.js file.

Instead of

const searchRepo = "../repositories/search.repository";

Do this

const searchRepo = require("../repositories/search.repository");
about 4 years ago · Juan Pablo Isaza 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!