我正在尝试将Tweets的文本(使用twitterapi)插入MongoDB数据库,我得到了
TypeError: Cannot create property _id on string
var Twitter = require('twitter');
var fs = require("fs");
var request = require("request");
var client = new Twitter({
consumer_key: ' ',
consumer_secret: ' ',
access_token_key: ' ',
access_token_secret: ' '});
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
var parser = { useNewUrlParser: true };
MongoClient.connect(url, parser, function(err, db) {
if (err) throw err;
console.log("Database created!");
var dbo = db.db("mydb");
dbo.createCollection("datos_sentimiento", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
MongoClient.connect(url, parser, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var cont = 0;
var params = {q: '@Avengers', count: 100, include_rts: 0, tweet_mode: 'extended', retweeted_status: 'extended'};
client.get('search/tweets', params, function(error, tweets, response) {
if (!error) {
//console.log(tweets);
for(var i=0; i < params.count; i++){
if(tweets.retweeted_status){
tweets = tweets.retweeted_status;
var tweet = (JSON.stringify(tweets.statuses[i])+"\n");
var string = tweet.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
console.log('hi');
}
else{
//console.log('hi2');
}
var tweet = (JSON.stringify(tweets.statuses[i].full_text));
var string = tweet.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
var string2 = (JSON.stringify(string)+"\n");
fs.appendFile("tweet.txt", string2, function (err) {
if (err) throw err;
//console.log('Saved!');
});
/*console.log(cont);
cont++;*/
var myobj = JSON.parse(string2);
console.log("hi "+myobj);
dbo.collection("datos_sentimiento").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
//console.log(i+'fdfdfdfdf');
}
}
});
});
错误显示以下消息:
TypeError: Cannot create property '_id' on string 'RT @RobertDowneyJr Brace yourselves @Avengers InfinityWar hits the interwebs today httpstcoqvBQ3W8jvc'