Home > Net >  Attempting to save data that is streamed from a twitter api
Attempting to save data that is streamed from a twitter api

Time:03-16

I am trying to save data from tweets to a mongoDB database using node and express.

I am using the twitter api to stream twitter data with a specific hashtags. I just want to save the text content of the post: Here is how the tweet content shows up when it is console.logged: (Note this feature works and this is my own posted

{
  created_at: 'Tue Mar 15 06:38:58  0000 2022',
  id: 1503621761388134400,
  id_str: '1503621761388134410',
  text: '#TelecomDisaster Test for project 2',
  source: '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>',
  truncated: false,
  in_reply_to_status_id: null,
  in_reply_to_status_id_str: null,
  in_reply_to_user_id: null,
  in_reply_to_user_id_str: null,
  in_reply_to_screen_name: null,
  user: {
    id: 1472188612494172200,
    id_str: '1472188612494172172',
    name: 'Dillon Rampersad',
    screen_name: 'R_Dillon_25',
    location: null,
    url: null,
    description: null,
    translator_type: 'none',
    protected: false,
    verified: false,
    followers_count: 5,
    friends_count: 11,
    listed_count: 0,
    favourites_count: 22,
    statuses_count: 63,
    created_at: 'Sat Dec 18 12:55:26  0000 2021',
    utc_offset: null,
    time_zone: null,
    geo_enabled: false,
    lang: null,
    contributors_enabled: false,
    is_translator: false,
    profile_background_color: 'F5F8FA',
    profile_background_image_url: '',
    profile_background_image_url_https: '',
    profile_background_tile: false,
    profile_link_color: '1DA1F2',
    profile_sidebar_border_color: 'C0DEED',
    profile_sidebar_fill_color: 'DDEEF6',
    profile_text_color: '333333',
    profile_use_background_image: true,
    profile_image_url: 'http://pbs.twimg.com/profile_images/1472188757956780033/OMlZZeZI_normal.jpg',
    profile_image_url_https: 'https://pbs.twimg.com/profile_images/1472188757956780033/OMlZZeZI_normal.jpg',
    default_profile: true,
    default_profile_image: false,
    following: null,
    follow_request_sent: null,
    notifications: null,
    withheld_in_countries: []
  },
  geo: null,
  coordinates: null,
  place: null,
  contributors: null,
  is_quote_status: false,
  quote_count: 0,
  reply_count: 0,
  retweet_count: 0,
  favorite_count: 0,
  entities: { hashtags: [ [Object] ], urls: [], user_mentions: [], symbols: [] },
  favorited: false,
  retweeted: false,
  filter_level: 'low',
  lang: 'en',
  timestamp_ms: '1647326338513'
}

I want to save text: '#TelecomDisaster Test for project 2', and created_at: 'Tue Mar 15 06:38:58 0000 2022', to my database. I am trying with the function below to save just the text for now but i dont quite understand how to:

const express = require('express')
const router = new express.Router();
var Twitter = require('twit')

const TwitterPosts = require("../db/models/TwitterPosts.model");

//api keys goes here but it removed for safety

var stream = client.stream('statuses/filter', { track: '#TelecomDisaster' })
stream.on('tweet', function (tweet) {
  console.log(tweet)
  let newtweet = new TwitterPosts({
    tweet: req.body.postContent
  });
  newtweet.save().then((twit) => {
    res.send(twit);
    console.log(twit);
})
});

module.exports = router;

The model for the schema:

const mongoose = require('mongoose');

const TwitterPostsSchema = new mongoose.Schema({
    twitterUsername:{
        type: String,
        required: false,
        minlength:1,
        trim: true
    },
    postContent:{
        type: String,
        required: false,
        minlength:1,
        trim: true
    },
    postDateTime:{
        type: Date,
        required: false,
        default: Date.now
    }
})

    
const TwitterPosts = mongoose.model( 'TwitterPosts', TwitterPostsSchema);

module.exports =  TwitterPosts

Whenever it trys to save i get the error

tweet: req.body.postContent ^ ReferenceError: req is not defined

i did not define req but in this use case i dont know how to do that exactly when streaming the tweets.

To conclude i am trying to save tweets to a mongoDB database using node and express. the tweets are streamed as shown above but i dont quite understand how it is saved to the database.

CodePudding user response:

you receive tweet in stream.on listener, so it's just tweet, instead of req.body.postContent:

 let newtweet = new TwitterPosts({
    tweet: tweet
  });

or, according to your schema:

 let newtweet = new TwitterPosts({
    twitterUsername: tweet.user.name,
    postContent: tweet.text,
    postDateTime: tweet.created_at 
  });
  • Related