0

I am trying to send data from Yelp API v3 to a Mongo database. I'm getting a response only issue is before it gets sent to the database it gets scrambled somewhere around :

  var objects = businesesObject[i];

  var newBusiness = Business();

This is what is saved to my database

 { _id: 5bf72505de908657e61ef900 }
 { _id: 5bf72505de9086727e619084 }
 { _id: 5bf7250e90849987841ef904 }
 { _id: 5bf72505de908427e6190847 }
 { _id: 5bf72505de908427e61ef999 }
 { _id: 5bf72505de908427e61ef90a }
 { _id: 5bf72505de908427567ef90c }
 { _id: 5bf72505de908427e61ef90e }
 { _id: 5bf72505de908423456ef910 }
 { _id: 5b567805de905427e61ef912 }

App.js

 var express = require('express');
 var path = require('path');
 var cookieParser = require('cookie-parser');
 var logger = require('morgan');
 var querystring = require('querystring');
 var indexRouter = require('./routes/index');
 var usersRouter = require('./routes/users');

 var app = express();



 require('./models/models');
 var mongoose = require('mongoose');
 mongoose.connect('mongodb://localhost/Businesses', { useNewUrlParser: true });
 var Business = mongoose.model('Business');


 // mongoose.connection.once('open', function(){
 //   console.log('database is cconnected');

 // }).on('error', function(error){
 //   console.log('connection error:', error);
 // })




 var Yelp = require('yelpv3');

 var yelp = new Yelp({
   apiKey: 'apikey'
 });



 // https://www.yelp.com/developers/documentation/v3/business_search


 yelp.search({ term: 'food', location: 'California', radius:40000  })
   .then(function (data) {
     var businesesObject = data;

     for(var i = 0; i< businesesObject.length;i++){

       var objects = businesesObject[i];

       var newBusiness = Business();


       newBusiness.is_claimed  = objects.is_claimed;
       newBusiness.rating  = objects.rating;
       newBusiness.review_count = objects.review_count;
       newBusiness.name = objects.objects;
       newBusiness.url = objects.url;
       newBusiness.categories = objects.categories;
       newBusiness.phone = objects.phone;
       newBusiness.image_url = objects.image_url;
       newBusiness.display_phone = objects.display_phone;
       newBusiness.id = objects.id;
       newBusiness.location = objects.location;

       // save the user
       newBusiness.save(function(err) {
           if (err){
               console.log('Error in Saving user: '+err);  
               throw err;  
           }
       });
     }
     console.log('Done saving to database.');

   })
   .catch(function (err) {
     console.error(err);
   });



 module.exports = app;

model.js

 var mongoose = require('mongoose');

 var Schema = mongoose.Schema;


 // Defining a schema for Business
 var businessSchema = new mongoose.Schema({
     name: String,
     image_url: String,
     url: String,
     review_count: String,
     categories:Object,
     rating : String,
     coordinates: Object,
     location: Object,
     phone: String,
     display_phone: String,
     transactions: String,
     is_claimed: Boolean,
     id: String,
 });

 mongoose.model('Business', businessSchema);
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
endlessCode
  • 1,255
  • 4
  • 21
  • 36
  • You are mixing callbacks and promises in a way you should not be doing. Methods like [`Document.save()`](https://mongoosejs.com/docs/api.html#document_Document-save) and all mongoose methods support Promises natively anyway. The "order" problem is further compounded by a `for` loop which is not awaiting async function completion. There are common ways to do that, as shown in the links. – Neil Lunn Nov 23 '18 at 02:56
  • sorry I am new to programming, and do don't fully understand those examples in the context of what I am doing. Can you please point out specifically using code example based on what I have provided. – endlessCode Nov 23 '18 at 19:43

0 Answers0