mongooseelectronmongoose-schemamongoose-models

Artist.findOne is not a function


I am getting the error inside my Electron application when I run this code inside my FindArtist.js file:

const Artist = require('../models/artist');

/**
 * Finds a single artist in the artist collection.
 * @param {string} _id - The ID of the record to find.
 * @return {promise} A promise that resolves with the Artist that matches the id
 */
module.exports = _id => {
  return Artist.findOne({ _id: _id });
};

I definitely installed mongoose, but it seems I am no longer importing mongoose or the Artist model appropriately? Not sure.

Here is the models/artist.js file:

const mongoose = require('mongoose');
const AlbumSchema = require('./album');
const Schema = mongoose.Schema;

const ArtistSchema = new Schema({
  name: String,
  age: Number,
  yearsActive: Number,
  image: String,
  genre: String,
  website: String,
  netWorth: Number,
  labelName: String,
  retired: Boolean,
  albums: [AlbumSchema]
});

const Artist = mongoose.model('artist', ArtistSchema);

module.exports = ArtistSchema;

Solution

  • I figure out that my module.exports on my models/artist.js file was wrong.

    Instead of:

    // const Artist = mongoose.model('artist', ArtistSchema);
    
    // module.exports = ArtistSchema;
    

    it should be this:

    module.exports = mongoose.model('artist', ArtistSchema);