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

236
Views
typeErorr can not read property findOne of undefined

I want to connect to external database exist on local via server.

In other words I want to get the Price Model which is exist in local mongodb database not in my server:

const mongooseLocal = require('mongoose');
const connectionOptions = { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false };
const conn = mongooseLocal.createConnection(config.MONGODB_URI_LOCAL, connectionOptions);
conn.on("error", console.error.bind(console, "connection error: "));
conn.once("open", async () => {

  console.log("Connected successfully");
  const priceModel = await conn.Price.findOne({}); // Price is a Model in  local side and not exist in my server . it's on local...
  console.log('init', priceModel);

});

The issue is although I see the "Connected successfully" log but I cann't get the data..

I see this error each time

typeErorr can not read property findOne of undefined

How can I fix this?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Since you're using mongoose your steps are:

  1. Define model schema ( https://mongoosejs.com/docs/index.html )
  2. Import model
  3. Use model's findOne method

Example:

// Price model (price.model.ts)
const mongoose = require('mongoose');

const priceSchema = new mongoose.Schema({
  name: String
});
exports.Price = mongoose.model('Price', priceSchema );

// index.ts (your file snippet)
const mongooseLocal = require('mongoose');
const { Price } = require('./price.model.ts');

const connectionOptions = { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false };
const conn = mongooseLocal.createConnection(config.MONGODB_URI_LOCAL, connectionOptions);
conn.on("error", console.error.bind(console, "connection error: "));
conn.once("open", async () => {

  console.log("Connected successfully");
  const priceModel = await Price.findOne({}); 
  console.log('init', priceModel);
});

about 4 years ago · Santiago Gelvez 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!