正在尝试保存从 Twitter 流式传输的数据 api

Attempting to save data that is streamed from a twitter api

我正在尝试使用 node 和 express 将推文中的数据保存到 mongoDB 数据库。

我正在使用推特 api 来流式传输带有特定主题标签的推特数据。我只想保存post的文本内容: 这是 console.logged 时推文内容的显示方式: (注意这个功能有效,这是我自己的 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'
}

我想将 text: '#TelecomDisaster Test for project 2',created_at: 'Tue Mar 15 06:38:58 +0000 2022', 保存到我的数据库中。 我正在尝试使用下面的功能暂时只保存文本,但我不太明白如何:

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;

模式的模型:

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

每当它尝试保存时我都会收到错误

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

我没有定义 req 但在这个用例中我不知道如何在流式传输推文时准确地做到这一点。

总而言之,我正在尝试使用 node 和 express 将推文保存到 mongoDB 数据库。推文的流式传输如上所示,但我不太明白它是如何保存到数据库中的。

您在 stream.on 侦听器中收到推文,所以它只是 tweet,而不是 req.body.postContent:

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

或者,根据您的架构:

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