2

I have tried everything I could think of but I'm still not able to insert any data into my MongoDB database. When receiving data (tweets) I log everything to check if the datatype matches the type of the field in my schema, which seems ok, but the data inserted into my database is just an empty mongodb document.

My schema:

var mongoose = require('mongoose');

var tweetSchema = new mongoose.Schema({
    userName: {type: String, required: true},
    imageUrl: {type: String},
    bannerUrl: {type: String},
    text: {type: String},
    hashTag: {type: String}
});

module.Exports = tweetSchema;

My model:

var mongoose = require("mongoose");
var tweetSchema = require("../schemas/tweet.js");

var Tweet = mongoose.model('Tweet',tweetSchema,"tweets");
module.exports = Tweet;

The part of my app where I try to insert the Document into my mongoDB:

var Twitter = require('node-tweet-stream');
require("./data/connectDB.js");
var Tweet = require('./data/models/tweet');

t.on('tweet', function(twdata){
    var uName = twdata.user.screen_name.toString();
    var iUrl = twdata.user.profile_image_url;
    var bUrl = twdata.user.profile_banner_url;
    var txt = twdata.text;
    var htag = "test";

    console.log("username: " + uName + " - " + typeof uName);
    console.log("image url: " + iUrl + " - " + typeof iUrl);
    console.log("banner url: " + bUrl + " - " + typeof bUrl);
    console.log("text: " + txt + " - " + typeof txt);
    console.log("hashtag: " + htag + " - " + typeof htag);



    var newTweet = new Tweet({
        userName: uName,
        imageUrl: iUrl,
        bannerUrl: bUrl,
        text: txt,
        hashTag: htag
    });
    console.log(newTweet);

    newTweet.save(function(err, newTweet){
        if(err){
            console.log(err);
        }
        else{
            console.dir(newTweet);
        }

    });

    //console.log(twdata.id);
});

The log I'm receiving of the datatypes and content:

username: ZBBXXR - string
image url: http://pbs.twimg.com/profile_images/550347979886845953/9aU5othN_normal.jpeg - string
banner url: https://pbs.twimg.com/profile_banners/274821783/1418814026 - string
text: RT @NamiZelmon: happy birthday yoseob oppa💕💕💕🎂🎁🎉🎊 @all4b2uty hope you like this🙈 #HappyYSDay http://t.co/C6W3LFg5F5 - string
hashtag: test - string

I don't get any errors but the Document inserted into my database is the following (which is just an empty mongoDB document):

{ __v: 0, _id: 54a9b1ba0114720c2711cbfc }
2
  • What happens if you try to insert with only the one required field? Commented Jan 4, 2015 at 22:27
  • Can you post twdata ? Commented Jan 4, 2015 at 22:46

1 Answer 1

3

I can't believe I didn't see this one. In my schema I wrote

module.Exports = tweetSchema;

while it should have been 'exports' non capitalized

module.exports = tweetSchema;

Because of the uppercase typo, nothing was returned when calling require('../schema/tweet.js') in './model/tweet.js. Hence, passing in undefined to the mongoose.model('Tweet',TweetSchema,"tweets") in './model/tweet.js' and creating empty database documents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.