1

I'm having trouble figuring out how to save a json object from a third party api to my personal localhost mongodb. I believe I'm supposed to create another method within the api controller but am not sure what kind of method should be used any help would be great thanks! Here is my code sorry if this is a dumb question.

//server.js
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var mongoose = require('mongoose');
var Router = require('./routes');
var morgan = require('morgan');
var port = process.env.PORT || 3000;

var app = express();

//database connect
mongoose.connect('mongodb://localhost/Emagispace')


app.use(
express.static('views'),
bodyParser.json(),
bodyParser.urlencoded({extended : true}),
morgan('dev')
);

Router(app);


app.listen(port, ()=>{
console.log(`Server running on ${port}`);

//Routes

var API = require('./controllers/api');

module.exports = (app)=>{

app.get('/', (req, res)=>{
    res.sendFile('index.html', {root : './views'});
});

app.get('/api/emagispace', API.product)
}

//API controller

var request = require('request-promise');
var baseURI = 'example';

module.exports = {

product : (req, res)=>{
    request({
        method : 'GET',
        url : `${baseURI}/api/emagispace/${req.query.products}`
    })
    .then((resp)=>{
        console.log('Product list : ', resp);
        res.send(resp).save();
    })
}
}
2
  • To save in mongoose, your going to want to call save on the mongoose object, not the response object. I'm not a mongoose user, but it might be something like -> mongoose.products.save(resp) Commented Feb 9, 2017 at 23:15
  • I'll give that a try thanks Keith Commented Feb 9, 2017 at 23:28

1 Answer 1

1

In order use mongoose for saving the documents you'll need to specify the Schema first.

//product-model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ProductSchema = new Schema({
	name:  String,
	price: String,
	//And all other data you need
});

var Product = mongoose.model('Product', ProductSchema);

Also you can do validation and much else. Please see the docs.

Then you can use .insertMany to save them in one shot or iterrate over your documents:

var Product = require('../models/product-model.js');

...

.then((resp)=>{
        //I assume that resp is an array of products here
	console.log('Product list : ', resp);

	//Go over your product list and save them
	for (var product of resp) {
		var p = new Product(product);
		p.save();
	}
  
        //OR
	Product.insertMany(resp);

	res.send(resp);
})

After this you'll have products collection in your local db. Also you can warp these calls into a method if you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Antonio this was exactly what I was looking for!
Hey Antonio I'm having issues with the loop saving to much data and I'm wondering is it possible to create an infinite for in loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.