Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose.connect undefined is not a function

I am trying to make a basic mongoose connection in c9.io using node.js, React, ReactRouter and webpack. I have my mongodb established and listening, but when I try to require mongoose and start a connection, it fails and tells me "undefined is not a function", pointing me to "mongoose.connect". Here is my code. I am not sure what I am doing wrong.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017');//this returns "undefined is not a function" and fails
var Schema = mongoose.Schema;

var postSchema = new Schema({
    title: String,
    description: String,
    author: String,
    content: String
});

var Post = mongoose.model('Post', postSchema);

module.exports = Post;

Any thoughts? I am wondering if it is a problem with webpack, since I'm using React-router instead of something server-side.

like image 348
Richard Herbert Avatar asked Apr 08 '15 05:04

Richard Herbert


1 Answers

If you are requiring this file in your client-side code (in a React component, for example), then yes, that's the problem.

You can only require this in the server-side (your "server.js" file, for example).

like image 139
Marcos Cassiano Avatar answered Sep 18 '22 00:09

Marcos Cassiano