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

时间:2018-07-25 04:32:26

标签: javascript node.js mongodb mongoose

I'm trying to learn NodeJS. I'm using mongoose & 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. Maybe it's not the way to do it in Node. Any help is super appreciated.

1 个答案:

答案 0 :(得分:0)

结果很简单。我只需要睡一晚。我需要连接到猫鼬,并在save()之后断开连接。

现在代码看起来像这样。我仍然需要在其中添加和编辑一些内容。任何聪明的重构建议,表示赞赏。

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);
  }
});

运行节点seed.js命令。 这是一般的想法。