我的 MongoDB 有問題。它沒有接收到我試圖發送給它的資料。一切似乎都很好,除了資料庫是空的。
我正在制作一個從 yahoo.finance 抓取資料的應用程式。
consts Price、Symbol 和 PercentChange 通過cheerio 獲取資料并請求承諾。如果我在控制臺記錄它們 - 他們將提供資料,我想將該資料存盤到 MongoDB 中。但是它是空的,如您在影像中看到的在底部
我使用串列作為 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();
這是我想將資料發送到的 ./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”,并且它不斷更新終端中的價格..
** 這是終端和空資料庫的影像 **
uj5u.com熱心網友回復:
首先檢查percentChange這個變數是否寫得很好,這可能是錯誤的,
const listingModel = new Listing({
price: Price,
symbol: Symbol,
percentchange: PercentChange,
});
然后你只需要保存資料,
listingModel.save();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457735.html
標籤:javascript 节点.js mongodb 猫鼬
上一篇:如何使用貓鼬模式保存字串陣列
下一篇:貓鼬查詢選擇父級和僅匹配的子級
