insert Json object from Yelp API [duplicate]












0















This question already has an answer here:




  • Correct way to write loops for promise.

    13 answers



  • JS wait for callback to finish inside a loop

    2 answers




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









share|improve this question















marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 2:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2: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.
    – obumoon
    Nov 23 at 19:43
















0















This question already has an answer here:




  • Correct way to write loops for promise.

    13 answers



  • JS wait for callback to finish inside a loop

    2 answers




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









share|improve this question















marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 2:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2: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.
    – obumoon
    Nov 23 at 19:43














0












0








0








This question already has an answer here:




  • Correct way to write loops for promise.

    13 answers



  • JS wait for callback to finish inside a loop

    2 answers




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









share|improve this question
















This question already has an answer here:




  • Correct way to write loops for promise.

    13 answers



  • JS wait for callback to finish inside a loop

    2 answers




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




This question already has an answer here:




  • Correct way to write loops for promise.

    13 answers



  • JS wait for callback to finish inside a loop

    2 answers








node.js mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 2:49









Neil Lunn

96.8k22170181




96.8k22170181










asked Nov 22 at 22:15









obumoon

325




325




marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 2:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 at 2:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2: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.
    – obumoon
    Nov 23 at 19:43


















  • You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2: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.
    – obumoon
    Nov 23 at 19:43
















You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2:56






You are mixing callbacks and promises in a way you should not be doing. Methods like 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 at 2: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.
– obumoon
Nov 23 at 19:43




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.
– obumoon
Nov 23 at 19:43

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)