Driff Driff - 3 months ago 9
Node.js Question

retrieving value from express router if value is a list/array

hey i got some problem getting the data in an API i'm making, here's the thing
in my model i want to store an array of items, it is a list of incoming products, so i'm testing the API and i'm getting some errors with the logic on my router, i'm sending the items through a post, all with the same key but different value, the console prints it the way i need them but when i try to get them into an array or even when i try to get one it gives me errors.

here's the model for the items:

var itemSchema = = new mongoose.Schema({
name: String,
details: String,
quantity: SchemaTypes.Double,
created_at: {type: Date, default: Date.now},
last_updated: {type: Date, default: Date.now}
}) mongoose.model('Item', itemSchema);


Here's the income model:

var itemsIncomeSchema = new Schema({
user: {type: schemaTypes.ObjectId, ref: 'User'},
sender: String,
created_at: {type: Date, default: Date.now()},
items: [Item]
}) mongoose.model('ItemIncome', itemsIncomeSchema);


i choose to make the income schema this way because i want to have a list of all the items but i want to have a registry of the ones that are entering so i didn't wanted to use a ref. (something like many to many)

anyway the problem is here when i'm testing the model through the API, i'm sending the values like this in a post:

sender=sender_name&items=57b694c626a4741c2f4bf6fd&items=57b694c626a4741c2f4bffff ...


and in the post method in my router i'm getting the data perfectly, it even prints on console but i have no idea how to make items an array or how to retrieve the data from items since i need to look for the _id's i'm sending, also not sure if i'm sending them correctly as i'm not sure if i'll pass only the _id or i'll pass the entire item.

here's the post:

.post(function(req, res){
var entry = new itemEntry();
var user = req.user;
//var items= req.body.items; <<tried like this but no success
console.log('items: '+req.body.items)
for (var item in req.body.items) {
console.log('item: '+item); //it prints item: 0...23 also tried with a toString
}
entry.user = user._id;
entry.items= items;
entry.save(function(err, entry){
if(err)
return res.send(500, err);
return res.json(entry);
});
})

Answer

What is ItemEntry? Is it supposed to be ItemIncome? In that case change your schema to reference the Item model:

items: [{type: schemaTypes.ObjectId, ref: 'Item'}]

Then in your code:

entry.items = req.body.items;

Update: Based on your comment I think your schema needs a little work. First you are not going to be able to create a new Item with the same id, that just won't work. But if your intention is to reference existing Items but have varying quantities then you want to adjust your schemas to not have the quantity field as part of Item but instead as part of the ItemIncome:

var itemSchema = = new mongoose.Schema({
  name: String,
  details: String,
  created_at: {type: Date, default: Date.now},
  last_updated: {type: Date, default: Date.now}
}) mongoose.model('Item', itemSchema);
Here's the income model:

var itemsIncomeSchema = new Schema({
   user: {type: schemaTypes.ObjectId, ref: 'User'},
   sender: String,
   created_at: {type: Date, default: Date.now()},
   items: [{
     quantity: quantity: SchemaTypes.Double,
     item: {type: schemaTypes.ObjectId, ref: 'Item'}
    }]
}) mongoose.model('ItemIncome', itemsIncomeSchema);

Your supplied data looks like:

items=57b694c626a4741c2f4bf6fd&items=57b694c626a4741c2f4bf6‌​ff&quantity=100&quan‌​tity=20" and now i'm recieving this " { items: [ '57b694c626a4741c2f4bf6fd', '57b694c626a4741c2f4bf6ff' ], quantity: [ '100', '20' ] }

Then:

var items = []
for(var i = req.body.items.length - 1; i >= 0; i --) {
  items.push({ quantity:  req.body.quantity[i], item: req.body.items[i] })
}
entry.items = items

I would recommend passing your data as a json object if you can. It will be potentially more robust as you can pass the quantity and item ID together rather than 2 separate arrays as you are doing now:

postdata: {
  items: [{
    id: '57b694c626a4741c2f4bf6fd',
    quantity: 100
  }, { ... }]
}

Express will have it available as an object in the req.body field.

If you need to create new Items then you will need to first create and save each Item, get their IDs then assign those. You can use the async library for this to help or use Promises.

Comments