cnak2 cnak2 -4 years ago 95
Node.js Question

I'm trying to figure out how you return all records when posting new?

I have an API where I want to make one trip to the db to both post a new record and then return all records that meet the criteria.

So here is my endpoint:

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

.post(function(req, res){
var product = new Product();

product.business_id = req.body.business_id;
product.product_name = req.body.product_name;
product.sku = req.body.sku;
product.description = req.body.description;
product.cost = req.body.cost; =;
product.donation = req.body.donation;
product.image = req.body.image;
product.post_date = req.body.post_date;
product.start_date = req.body.start_date;
product.end_date = req.body.end_date;
product.available = req.body.available;, product){

I want to then return all the records that have the same "business_id"

The problem is it only returns the record I just post.

Any advice?

Answer Source

I'm not sure that returning the list is good from the REST perspective. I would probably make an additional call to get the list just to keep it clean architecture wise.

But anyway, to achieve what you want you'll definitely need to make another DB call and retrieve the needed records like this:, product){
	if(err) {
		//You could send a status here like this: res.status(400).send(err) here.

	Product.find({business_id: req.body.business_id}, function (err, data) {
		if (err) {


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download