简体   繁体   中英

Insert data into array of schema with NodeJS and mongoose

I have the next Schema:

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = Schema({
  id: String,
  name: [{
    name: String,
    surname1: String,
    surname2: String,
  }]
});

module.exports = mongoose.model('User', userSchema);

And I created the next function:

module.exports.signUp = function(req,res){
  console.log('POST signUp');
  let user = new User();

  for(var key in req.body) {
    console.log(key);
    switch (key) {
      case 'name[name]':
        user.name.push({name:"name"});
      break;
      case 'name[surname1]':
        user.name.push({surname1:"surname1"});
      break;
      case 'name[surname2]':
        user.name.push({surname2:"surname2"});
      break;
    }
  }         
  res.status(202).send({message: user});
}

I need the next results:

{
    "message": {
        "_id": "5b61e242c4874a045dd4185a",
        "name": [
            {
                "name": "name",
                "surname1": "surname1",
                "surname2": "surname2"
            }
        ]
    }
}

And I get:

{
    "message": {
        "_id": "5b61e242c4874a045dd4185a",
        "name": [
            {
                "_id": "5b61e242c4874a045dd4185b",
                "name": "name"
            },
            {
                "_id": "5b61e242c4874a045dd4185c",
                "surname1": "surname1"
            },
            {
                "_id": "5b61e242c4874a045dd4185d",
                "surname2": "surname2"
            }
        ]
    }
}

It generates multiples _id and different JSON format.

I tried with user.name[0]="name", user.name.name="name" but It doesn't work... When I tried user.name = {"name":"name"} works, but after that I put user.name = {"surname1":"surname1"} and the name doesn't exists.

Can you help me please? Thank you!

You're looping through req.body and pushing a key each time. You need to be doing it all at once. Something like

const name={};
if ('name[name]' in req.body){name.name=req.body['name[name]'];}
if ('name[surname1]' in req.body){name.surname1=req.body['name[surname1]'];}
if ('name[surname2]' in req.body){name.surname2=req.body['name[surname2]'];}
user.name.push(name);

Each entry in the name array will have a separate id though because they're declared as a subdocument in Mongo. You'll still be able to access the user by its id, but you'll also have access to each name object individually.

If each user just needs one name though, you can simplify the schema to

const userSchema = Schema({
  id: String,
  name: {
    name: String,
    surname1: String,
    surname2: String,
  }
});

and then when you have made the name object do

user.name=name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM