2

I want to save/insert each Object from the Array into my MongoDB, after receiving it from a Facebook endpoint.

How would I iterate over the returned Array of Objects and then save it to my mongoDB? :)

Code from my Route, where I receive the data from the Facebook API with superagent

var express = require('express');
var router = express.Router();
var request = require('superagent')
var utils = require('../utils')

router.get('/facebook', function(req, res, next) {

  var url = "https://graph.facebook.com/v2.9/xxxxxxxx"

  request
    .get(url)
    .set('Accept', 'application/json')
    .end(function(err, result){
      var parse = JSON.parse(result.text)
      res.json({
        confirmation: 'success',
        response: parse
      })
    });

});

module.exports = router;

Each Object looks like this:

"confirmation": "success",
"response": {
"data": [
{
"message": "Søger  et godt hjulsæt til Max 1000\nkr.1,000 - Løsning, Vejle, Denmark\n\nVed at ser er en masse fine hjulsæt er er i overskud der ude er der nogen der har et rigtig fint hjulsæt  i vil sælge mig ?",
"from": {},
"permalink_url": "https://www.facebook.com/groups/306677009409252/permalink/1333896396687303/?sale_post_id=1333896396687303",
"type": "photo",
"created_time": "2017-04-25T14:26:39+0000",
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/q81/s130x130/18157109_10155237818408609_6730263000530603997_n.jpg?oh=e4198809641ce4549592065f9ce1d522&oe=5993A162",
"id": "306677009409252_1333896396687303",
"attachments": {}
},
{
"message": "Bontrager cykelhjelm\nkr.250 - Lemvig\n\nNy cykelhjelm  bontrager starvos str S\n51-57 cm ny og i original emballage. Ny pris 649 kr",
"from": {
"name": "Tanja Olsen",
"id": "10155372412188754"
},
"name": "Photos from Tanja Olsen's post",
"permalink_url": "https://www.facebook.com/groups/306677009409252/permalink/1333870013356608/?sale_post_id=1333870013356608",
"type": "photo",
"created_time": "2017-04-25T13:49:26+0000",
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/18118873_10155371971413754_3345993522211660552_n.jpg?oh=de0d10dc9e0589789e023819402ef399&oe=598FAE09",
"id": "306677009409252_1333870013356608",
"attachments": {}
}]

My Model/Schema

var mongoose = require('mongoose')

var FeedSchema = new mongoose.Schema({
    type: {type: String, default: ''},
    title: {type: String, default: ''},
    image: {type: String, default: ''},
    description: {type: String, default: ''},
    timestamp: {type: Date, default: Date.now}
})

module.exports = mongoose.model('FeedSchema', FeedSchema)

What I have attempted but didnt work yet

var express = require('express');
var router = express.Router();
var request = require('superagent')
var utils = require('../utils')
var Feed = require('../models/Feed')

router.get('/facebook', function(req, res, next) {

  var url = "https://graph.facebook.com/v2.9/XXXXXXX"
  var bulk = Feed.initializeUnorderedBulkOp();

  request
    .get(url)
    .set('Accept', 'application/json')
    .end(function(err, result){
      var parse = JSON.parse(result.text)

      var object = parse.data

      array.forEach((object) => {
        bulk.insert({
          type: object.type,
          title: object.title,
          image: object.image,
          description: object.description
        })
      })
      bulk.execute();
  });


});

module.exports = router;
DbTheChain
  • 225
  • 2
  • 4
  • 13
  • Why are you iterating over the object, are you looking to alter the objects before inserting them into you db. Also what is you model schema like? is there a reason why you cant use , $pushAll' 'Bulk.insert()' ? – Benji Lees Apr 25 '17 at 19:24
  • I want to save each object from the array, then I have to iterate over each object right or is it a wrong approach? I updated the post with the model – DbTheChain Apr 25 '17 at 19:29
  • ok it sounds like you want to map over the array and return an array of objects with the properties that you require. You can then do a bulk.insert to insert all of the objects at once. see https://docs.mongodb.com/manual/reference/method/Bulk.insert/ – Benji Lees Apr 25 '17 at 19:31
  • Oh cool, but what if i wanted to alter the data before it is saved, then I cant use bulk right? – DbTheChain Apr 25 '17 at 19:35
  • Check the answer i created – Benji Lees Apr 25 '17 at 19:38

1 Answers1

0

You can iterate over the array and then bulk.execute all the objects.

var bulk = db.items.initializeUnorderedBulkOp();
array.forEach((object) => {
  bulk.insert({
    type: object.type,
    title: object.title,
    image: object.image,
    description: object.description,
    timestamp: object.timestamp
  })
})
bulk.execute();
Benji Lees
  • 815
  • 6
  • 12
  • Awesome thanks, and were in my route should I place it, after .end? Also in the var bulk = db.items.initializeUnorderedBulkOp(), I should replace the db with my model or database? – DbTheChain Apr 25 '17 at 19:42
  • Yes too both. Just do it directly after the data is returned. – Benji Lees Apr 25 '17 at 19:46
  • Cool thanks, I tried what you suggested, but I cant seem to get it to work, could you check the code I added in the post? :) – DbTheChain Apr 25 '17 at 20:02
  • you still need to respond i,e: `res.send(result.text)` and the `array` should be your array I just used array as a place holder. im guessing that parse is the array – Benji Lees Apr 25 '17 at 20:08
  • and then how to return from this? res.send(json etc) – zero_cool May 08 '18 at 04:12
  • @zero_cool bulk.execute() returns a promise, you can return `res.send(json etc) ` in the then block – Benji Lees May 10 '18 at 10:40