Querying mongodb via nodejs shows empty result?

Viewed 44

1.I am trying to query the collection "radar_life_cycle" in database "teamautomation" via node below.js as below

2.I created the corresponding model ,connect to mongodb via mongoose is successful ,there are no errors but I get an empty list.

  1. As you can see from output below posts is [] is empty list but I can see that there are records in the database,can anyone provide guidance on what could be wrong here?

app.js

const express = require("express");
const mongoose = require("mongoose");
const Radar_life_cycle = require("./models/radar_life_cycle");
const app = express();

mongoose
  .connect(
    "mongodb://username:password@1x.xxx.xxx.x:27017/wifiautomation"
  )
  .then(() => {
    console.log("Connected to database!");
  })
  .catch(() => {
    console.log("Connection failed!");
  });


app.get("/api/radars", (req, res, next) => {
 Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
    res.status(200).json({
      message: "Posts fetched successfully!",
      posts: documents
    });
  });
});

models/radar_life_cycle

const mongoose = require('mongoose');
const radar_life_cycle_Schema = mongoose.Schema({
    Delivered: String,
    orgRadar: String,
    root_build: String,
    inserted_by: String,
    milestone: String,
    applicable_chipsets: [String],
    project_tag: String,
    gerrits:String,
    inserted_on:Date,
    SDK:String
});

module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);

OUTPUT:-

terminal$ nodemon server.js
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
(node:17500) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected to database!

{"message":"Posts fetched successfully!","posts":[]}

Expected output:-

{"message":"Posts fetched successfully!","posts":should include the corresponding records}

Screenshot of document in database:

enter image description here

2 Answers

Try the Schema in this way maybe is not doing a good reference to the collection

const { Schema } = require('mongoose');

const radar_life_cycle_Schema= new Schema({
    Delivered: String,
    orgRadar: String,
    root_build: String,
    inserted_by: String,
    milestone: String,
    applicable_chipsets: [String],
    project_tag: String,
    gerrits:String,
    inserted_on:Date,
    SDK:String
},
{
  collection: 'radar_life_cycle',
  timestamps: { createdAt: true, updatedAt: true },
});

module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);
app.get("/api/radars", (req, res, next) => {
 Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
    res.status(200).json({
      message: "Posts fetched successfully!",
      posts: documents[0]
    });
  });
});

find method finds all documents in the database in this case it seems there is only one... If there are more you can update code above and access the documents using a loop.

Related