factory-girl: TypeError: Model is not a constructor (mongoose

factory-girl version 4.2.2

I think this is related to #63. I am using Mongoose but otherwise used the same set up suggested there.

var fg = require('factory-girl');

var adapter = new fg.MongooseAdapter();
factory = fg.factory;
factory.setAdapter(adapter);

When running npm test I get:

TypeError: Model is not a constructor
    at MongooseAdapter.build (/Users/stoebelj/freecodecamp/voting/node_modules/factory-girl/index.js:703:14)
    at Factory._callee2$ (/Users/stoebelj/freecodecamp/voting/node_modules/factory-girl/index.js:155:33)
    at tryCatch (/Users/stoebelj/freecodecamp/voting/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/stoebelj/freecodecamp/voting/node_modules/regenerator-runtime/runtime.js:337:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/stoebelj/freecodecamp/voting/node_modules/regenerator-runtime/runtime.js:96:21)
    at step (/Users/stoebelj/freecodecamp/voting/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/stoebelj/freecodecamp/voting/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

Am I missing something?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

In my case I cleared many errors including this one after correcting this: module.export to module.exports 😊

In my case I cleared many errors including this one after correcting this: module.export to module.exports 😊

And I spent an hour or so just to find out that I wrote exports.module instead of module.exports…

Sometimes is the way you call the model from the controller.

If you call it like below, then it will tell you that kind of issues const { modelRes }= require(“…/models/anyModel”);

But if instead you use something like without the braces:

const modelRes = require(“…/models/anyModel”);

Then it should work fine.

Regards,

I have the same issue

const thingSchema = mongoose.Schema({
name: String, category:String});
module.export = mongoose.model('Thing',thingSchema)

When I call const something = new Thing({name:'Stone',category:'objects'}); I get TypeError: Thing is not a constructor

has anyone found a fix?

I have the same experience, found out I forget the (s) on module.exports.

here also i’m facing the same situation ,can anyone solve this?

this is the code: `var mongoose=require(“mongoose”); mongoose.connect(“mongodb://localhost/catapp”);

var catdetails = new mongoose.details({ name:String, age:Number, breed:String }); var Cat=mongoose.model(“Cat”,catdetails);

var george=new Cat({ name:“george”, age:11, breed:“grouchy” }); george.save(function(err,cat){ if(err){ console.log(“something Wrong”); } else{ console.log(“we had done it!”); console.log(cat); } });`

this is error:

TypeError: mongoose.details is not a constructor at Object.<anonymous> (/home/ubuntu/workspace/database/cat.js:4:18) at Module._compile (module.js:570:32) at Object.Module._extensions…js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:389:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:504:3