I have a problem with my MongoDB. It's not receiving data that I am trying to send into it. Everything seems to work fine, except the database is empty.
I'm making an app that scrapes data from yahoo.finance.
consts Price, Symbol and PercentChange get that data thru cheerio and request promise.. if I console log them - they will provide the data, and I would like to store that data onto MongoDB.. however it's empty as you can see in the image on the bottom
I'm using Listing as the name for the Model/Schema of the MongoDB
const request = require("request-promise");
const fs = require("fs");
const mongoose = require("mongoose");
const cheerio = require("cheerio");
const Listing = require("./model/Listing");
async function connectToMongoDB() {
await mongoose.connect("mongodb srv://********:********@cluster0.ctjxj.mongodb.net/Cluster0?retryWrites=true&w=majority");
console.log("connected to mongodb");
};
async function tsla() {
const html = await request.get(
"https://finance.yahoo.com/quote/tsla/"
);
const $ = await cheerio.load(html);
const Price = $('[]').text();
const Symbol = $('[]').text();
const PercentChange = $('fin-streamer[][data-field="regularMarketChangePercent"]').find('span').text();
await connectToMongoDB();
const listingModel = new Listing({
price: Price,
symbol: Symbol,
percentchange: PercentChange,
});
console.log(Price)
setTimeout(tsla, 20000); // 60,000 == 60 seconds == 1minute
}
tsla();
and here's my ./model/Listing.js file that I would like to send my data into
const mongoose = require("mongoose");
const listingSchema = new mongoose.Schema({
symbol: String,
price: String,
percentChange: String,
});
const Listing = mongoose.model("Listing", listingSchema);
module.exports = Listing;
For some reason I'm not getting the data, altho it shows "connected to mongodb" as well as it continually updates the prices in the terminal..
** Here's an image of terminal and the empty database **
CodePudding user response:
First check that percentChange
is well written in this variable, this may be the mistake,
const listingModel = new Listing({
price: Price,
symbol: Symbol,
percentchange: PercentChange,
});
then you just have to save the data,
listingModel.save();