Skip to content

Instantly share code, notes, and snippets.

@merolhack
Created August 25, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merolhack/cb6caa6a3dca7eb0740d3c750ec5c608 to your computer and use it in GitHub Desktop.
Save merolhack/cb6caa6a3dca7eb0740d3c750ec5c608 to your computer and use it in GitHub Desktop.
Read the user input
const mongoClient = require('mongodb').MongoClient;
const rl = require('readline');
const url = 'mongodb://localhost:27017';
const dbName = 'foo-bar';
const TWEETS_COLLECTION = 'tweets';
const USERS_COLLECTION = 'users';
const ask = function(question, callback) {
var r = rl.createInterface({
input: process.stdin,
output: process.stdout
});
r.question(question + '\n', function(answer) {
r.close();
callback(answer);
});
};
ask('Ingresa el nombre del autor:', function(authorName) {
console.log('Nombre:', authorName, '\n');
ask('Ingresa el texto del tweet:', function(text) {
console.log('Texto:', text, '\n');
mongoClient.connect(url, function(error, client) {
if (error) console.log('error:', error);
console.log('Conexión exitosa!\n');
const db = client.db(dbName); // use foo-bar;
const tweetDate = new Date();
db.collection(TWEETS_COLLECTION).insert({
author: authorName,
tweet: text,
date: tweetDate,
});
db.collection(TWEETS_COLLECTION).find({author: authorName}).project({'_id': false, 'author': true}).toArray(function(error, result) {
if (error) console.log('error:', error);
console.log('result:', result);
});
client.close();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment