MongoDB 未将数据接收到 Model/Schema
MongoDB not receiving data into Model/Schema
我的 MongoDB 有问题。它没有收到我要发送给它的数据。一切似乎都正常,除了数据库是空的。
我正在开发一个从 yahoo.finance.
抓取数据的应用
consts Price、Symbol 和 PercentChange 通过 cheerio 获取数据并请求 promise.. 如果我控制台记录它们 - 它们将提供数据,我想将该数据存储到 MongoDB.. 但是它是空的,正如您在底部图片中看到的那样
我使用 Listing 作为 MongoDB
的 Model/Schema 的名称
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 = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
const Symbol = $('[class="D(ib) Fz(18px)"]').text();
const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][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();
这是我的 ./model/Listing.js 文件,我想将我的数据发送到
const mongoose = require("mongoose");
const listingSchema = new mongoose.Schema({
symbol: String,
price: String,
percentChange: String,
});
const Listing = mongoose.model("Listing", listingSchema);
module.exports = Listing;
出于某种原因我没有得到数据,尽管它显示“连接到 mongodb”并且它不断更新终端中的价格..
**
Here's an image of terminal and the empty database
**
首先检查percentChange
这个变量写的好不好,这可能是错误的,
const listingModel = new Listing({
price: Price,
symbol: Symbol,
percentchange: PercentChange,
});
那么你只需要保存数据,
listingModel.save();
我的 MongoDB 有问题。它没有收到我要发送给它的数据。一切似乎都正常,除了数据库是空的。
我正在开发一个从 yahoo.finance.
抓取数据的应用consts Price、Symbol 和 PercentChange 通过 cheerio 获取数据并请求 promise.. 如果我控制台记录它们 - 它们将提供数据,我想将该数据存储到 MongoDB.. 但是它是空的,正如您在底部图片中看到的那样
我使用 Listing 作为 MongoDB
的 Model/Schema 的名称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 = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
const Symbol = $('[class="D(ib) Fz(18px)"]').text();
const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][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();
这是我的 ./model/Listing.js 文件,我想将我的数据发送到
const mongoose = require("mongoose");
const listingSchema = new mongoose.Schema({
symbol: String,
price: String,
percentChange: String,
});
const Listing = mongoose.model("Listing", listingSchema);
module.exports = Listing;
出于某种原因我没有得到数据,尽管它显示“连接到 mongodb”并且它不断更新终端中的价格..
** Here's an image of terminal and the empty database **
首先检查percentChange
这个变量写的好不好,这可能是错误的,
const listingModel = new Listing({
price: Price,
symbol: Symbol,
percentchange: PercentChange,
});
那么你只需要保存数据,
listingModel.save();