1

Im building a web app backend server using node.js, with the help of the mongoose and express libraries. my code listnes on route "/" using express.Router().get(), and when a "get request" was recived it fetches the data from the mongodb collection using mongoose.model.find() and sends the data back.

the problem is that no matter what i have tried, mongoose.model.find() returns an empty array...

this is the code of express.Router().get():

const express = require("express");
const router = express.Router();

const AttackPattern = require("../models/attack_pattern"); //the model

router.get("/", (req, res) => {
  AttackPattern.find({}, function (err, docs) {
    if (err) {
      console.log("error!"); //there was an error...
    } else {
      console.log(docs); //fetch succeful
      res.status(200).send(docs);
    }
  });
});

and this is the code of the model:

const mongoose = require("mongoose");

const attackPatternSchema = mongoose.Schema({
  _id: String,
  name: String,
  description: String,
  x_mitre_platforms: [String],
  x_mitre_detection: String,
  phase_name: String,
});

module.exports = mongoose.model(
  "AttackPattern",
  attackPatternSchema,
  "attack_pattern"
);

i have already looked at Model.find() returns empty in mongoose and Mongoose always returning an empty array NodeJS but found no luck...

IMPORTANT TO KNOW:

  1. The name of the collection is "attack_pattern" matching the third parameter of mongoose.model().
  2. The Schema's fields names and types match the documents of the collection's fields names and types.
  3. The connection to the mongodb cluster is succesful (established in another file).
  4. The field _id is of type string, not ObjectId (the documents _id field still have a unique value, but its not autoly generated).

help will be appreciated :)

1 Answers1

0

In your model delete param attack_pattern

module.exports = mongoose.model(
  "AttackPattern",
  attackPatternSchema
);

or

when you create your model, change mongoose.model by new Schema and declare _id attribute like:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var attackPatternSchema = new Schema({
    _id: { type: Schema.ObjectId, auto: true },
    // others attributes
})
max
  • 469
  • 3
  • 10
  • unfortunalety it did'nt fixed the problem... any other suggestions? – אייל היינריך May 18 '20 at 13:36
  • @איילהיינריך I changed my answer, cheeck it – max May 18 '20 at 13:59
  • @איילהיינריך if you desired declared `_id` must be `new Schema({ _id: { type: Schema.ObjectId, auto: true }})`, I changed again my answer, cheeck it. – max May 18 '20 at 14:45
  • did'nt worked eather... did'nt mentioned it but field _id is of type string, not objectId (the documents _id field still have a unique value, but its not autoly generated). – אייל היינריך May 18 '20 at 19:25
  • ok, try change your `_id` to other name like `idAttack` or just `id`, that can be error, read https://github.com/Automattic/mongoose/issues/1285 – max May 18 '20 at 19:39
  • now im really frustrated... i've changed the document's of the collection to have an auto-selected _id field of type ObjectId, and added field id of type string (holds the values of field _id from the old docs), changed the schema correspondingly and still no success :( – אייל היינריך May 20 '20 at 19:05
  • jaja take it easy my friend, put some logs error to can help you – max May 21 '20 at 01:45