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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With