简体   繁体   English

如何使用来自外部API的数据为mongodb播种?

[英]How do I seed mongodb with data from an external API?

I'm trying to learn NodeJS. 我正在尝试学习NodeJS。 I'm using mongoose & mLab. 我正在使用猫鼬和mLab。 I'm new to every one of these technologies. 我对这些技术中的每一项都是新手。

My model at the moment looks like this. 目前,我的模型是这样的。 I will add a few things to the schema later. 稍后,我将向架构添加一些内容。

const mongoose = require("mongoose");
const fetchData = require("../seed");

const schema = mongoose.Schema;

const dataSchema = new Schema({});

module.exports = recallData = mongoose.model("recalls", dataSchema);

I also made a seed file for fetching data.. 我还制作了一个种子文件来获取数据。

const Recall = require("./models/Recall");

 module.exports = function getData(req, res) {
  const urls = [url1, url2, url3];

  urls.map(url => {
    fetch(url)
      .then(res => res.json())
      .then(data =>
        data.results.map(recalls => {
          let recs = new Recall(recalls);
          recs.save;
        })
      );
  });
}

my question is how do I make the fetch run and populate the database? 我的问题是如何使获取运行并填充数据库? Is there a command or a mongoose function that will do that? 有命令或猫鼬功能可以做到这一点吗? I know that I'm basically trying to emulate Rails with a seed file. 我知道我基本上是在尝试使用种子文件来模拟Rails。 Maybe it's not the way to do it in Node. 也许这不是在Node中完成的方法。 Any help is super appreciated. 任何帮助都非常感谢。

Turns out it's pretty simple. 事实证明这很简单。 All I needed was a nights sleep. 我只需要睡一晚。 I needed to connect to mongoose and after save() , disconnect. 我需要连接到猫鼬,并在save()之后断开连接。

Now the code looks like this. 现在,代码如下所示。 I still need to add and edit some stuffs in it. 我仍然需要在其中添加和编辑一些内容。 Any smart refactoring advice is appreciated. 任何聪明的重构建议,表示赞赏。

const mongoose = require("mongoose");
const Recall = require("./models/Recall");
const db = require("./config/keys").mongoURI;
const fetch = require("node-fetch");
const URLS = require("./config/seedURLs");

let resultData;
let saveCounter = 0;

mongoose
  .connect(db)
  .then(() => console.log("mongodb connection success"))
  .catch(error => console.log(error));

URLS.map(async url => {
  try {
    const response = await fetch(url);
    const json = await response.json();
    resultData = [...json.results];

    for (let i = 0; i < resultData.length; i++) {
      let temp = new Recall({
        key1: resultData[i].key1,
        key2: resultData[i].key2,
        .
        .
        .
      });

      temp.save(() => {
        saveCounter++;
        if (saveCounter === resultData.length) {
          mongoose
            .disconnect()
            .then(() => console.log("mongodb disconnected"))
            .catch(error => console.log(error));
        }
      });
    }
  } catch (error) {
    console.log(error);
  }
});

Run node seed.js command. 运行节点seed.js命令。 This is the general idea. 这是一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM