【问题标题】:Post data to Subdocument - Mongoose Express将数据发布到子文档 - Mongoose Express
【发布时间】:2021-05-16 20:36:21
【问题描述】:

谷歌搜索后!...

使用 Mongoose/Express 保存数据我遇到了问题: 无法将数据发布到子文档或嵌套文档

这是我的猫鼬模型:

const Users = new Schema({
    firstname : {
        type : String
    },
    password : {type : String},
    phone : {type : Number},
    city : {type : String},
    country : {type : String},
    experience : [
        {
            title : {type : String},
            company : {type : String},
            description : {type : String},
            date_bg : {type : Date},
            date_end : {type : Date},
        }
    ],
    education : [
        {
            degree : {type : String},
            school : {type : String},
            description : {type : String},
            date_bg : {type : Date},
            date_end : {type : Date},
        }
    ],
// ...
})

这里是后路由器:

const addUser = async (req, res) => {

//    my data from fronted is like this :
 
// req.body.experience is like this (same as education): 
// [{title: "title experince 1", company: "title experince", date_bg: "2021-02-11", date_end: "2021-02-02", description: "title experince"}
{title: "title experince 2", company: "title experince 2", date_bg: "2021-02-10", date_end: "2021-02-08", description: "tstset"}]
    const user = new Users(req.body) // This cause cast error for the *experience* and *education*
    try {
        await user.save()
        return res.status(200).json({message : "Signin up successfully, Your account is now active!"})
    } catch (error) {
        return res.json({error : "Something went wrong, please try again later!", message : error})
    }
}

我的问题是:

如何将我的数据发布到子文档?(我的问题在于经验和教育模型)

【问题讨论】:

    标签: node.js express mongoose


    【解决方案1】:

    这是我收到的数据,但实际上无法保存:

    {
      email: 'manana04@gmail.com',
      password: '$2a$10$DSpWahC5QCDEWwr1VljOXeok3awr/vVzDRcGazQsevu3H1ZgNY.pm',
      firstname: 'dkfsdgk',
      lastname: 'kvhdvgdjk',
      job_title: 'cbhckvk5',
      description: 'sdfvbilfgad',
      hourly_rate: '454',
      city: 'sdkvhdfvik',
      country: 'hsvhdo',
      education: [],
      experience: [
        {
          title: 'Title experincev v',
          company: 'dfvbhdfighfi',
          date_bg: '2021-02-04',
          date_end: '2021-02-17',
          description: 'sdfsfsdfs'
        },
        {
          title: 'tititititititititititititit',
          company: 'dfldhkfjshdkfhsdkfshk',
          date_bg: '2021-02-04',
          date_end: '2021-02-16',
          description: 'sdfshfdslfsdhfhs'
        }
      ]
    }
    
    

    【讨论】:

      【解决方案2】:

      req.body.experience request 的 req.body.experience 在第二次体验之前有一个1 所以改变body的格式如下:

      [
        {
          title: "title experince 1",
          company: "title experince",
          date_bg: "2021-02-11",
          date_end: "2021-02-02",
          description: "title experince"
        },
        {
          title: "title experince 2",
          company: "title experince 2",
          date_bg: "2021-02-10",
          date_end: "2021-02-08",
          description: "tstset"
        }
      ]
      

      【讨论】:

      • 你的方法是对的,然后创建一个新模型并保存它,但是如果你想在经验和教育列表中添加新的经验或教育,你应该使用 $push 到数组
      • 不客气。如果这回答了您的问题,请接受它,这样我们都可以获得更多的声誉,其他用户可以看到这个答案解决了您的问题。看到这个链接stackoverflow.com/help/someone-answers
      • 您好 - 还有一个问题 - 我的帖子实际上是有效的,但不是在 try 块中返回 res.json({message : 'ddd'}) 而是在 catch 块中返回错误而不打印错误。我尝试显示错误,但它在 console.log(error) 中为空
      • req.body 正确保存在集合中??删除res之前的返回并再次检查它,并为catch块设置一个状态码,如res.status(500).json(),在res.status.json()之前尝试console.log(错误)跨度>
      • 谢谢 - 我确实更改了我的代码并忘记恢复和检查
      猜你喜欢
      • 2019-10-09
      • 2014-02-10
      • 2016-06-14
      • 2017-09-25
      • 2021-02-16
      • 2015-06-02
      • 2018-04-01
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多