简体   繁体   中英

TypeError: Parameter url must be string, not object

Came across this error, but i am very confused as to why i'm getting it because the url i'm using in my twitterClient.get is a string. I've tried chaning ports and restarting the computer, as well as searching for any syntax errors to no avail. Any help would be greatly appreciated!

'use strict';

const express = require('express'),
    request = require('request'),
    twitter = require('twitter'),
    aryMod = require('./resources/modules/objectArrayMod.js'),
    app = express();

app.set('view engine', 'pug');
app.set('views', './views');

app.use(express.json());
app.use(express.urlencoded({
    extended: true
}));

app.use(express.static('resources')); 

app.get('/', function(req, res) {

    let twitterClient = new twitter({
        consumer_key: '//////',
        consumer_secret: '//////',
        access_token_key: '//////',
        access_token_secret: '//////'
    });

    twitterClient.get({
        url: 'https://api.twitter.com/1.1/search/tweets.json',
        qs: {
            q: '#csc365'
        },
        json: true
    }, function(err, response, body) {
        let tweetAry = [];

        for(let i = 0; i < body.statuses.length; i++) {
            let object = body.statuses[i];
            let text = unescapeText(object.text);

            if(text !== ''){
                let repSent = text.replace(/[^a-zA-Z ]/gi, ''),
                    lSent = repSent.toLowerCase(),
                    sentenceAry = lSent.split(' '),
                    numOfAnagrams = 0;

                for(i = 0; i < sentenceAry.length; i++){
                    sentenceAry[i] = sentenceAry[i].split('').sort().join();
                }

                for(i = 0; i < sentenceAry.length; i++){
                    let anagram = false;

                    if(sentenceAry[i] !== -1){
                        for(let j = i + 1; j < sentenceAry.length; j++){
                            if(sentenceAry[j] !== -1){
                                if(sentenceAry[j] === sentenceAry[i]){
                                    numOfAnagrams++;
                                    sentenceAry[j] = -1;
                                    anagram = true;
                                }
                            }
                        }
                    }

                    if(anagram){
                        numOfAnagrams++;
                    }

                    sentenceAry[i] = -1;

                    tweetAry.push({
                        text: text,
                        anaScore: numOfAnagrams
                    });
                } 
            }   
        }

        aryMod.aryOfObjects(tweetAry);

        if (!err) {
            res.render('twitter', { aryMod: aryMod });
        }
        else {
            console.error(err);
        }
    });
});

let unescapeText = function(escTxt) {
        return escTxt.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
    };

const server = app.listen(3000, function () {
        console.log(`Started server on port ${server.address().port}`);
    });

What this server is supposed to do is dynamically use tweets containing a specific hashtag,count up the amount of anagrams per tweet, where a separate AJAX file comes in and load them to the page, lets you click them,and keeps score of the amount of anagrams on those tweets

Looking at the node-twitter docs, it seems you should be calling twitter.get() with a url string as the first argument, and then an object containing your parameters, like so: twitterClient.get('search/tweets', params, function(err, response, body) {...})

Here's an example from their docs on how you would use the node client to search tweets:

client.get('search/tweets', {q: 'node.js'}, function(error, tweets, response) {
   console.log(tweets);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM