【问题标题】:Nested MongoDB document issue (Mongoose and Node Js)嵌套 MongoDB 文档问题(Mongoose 和 Node Js)
【发布时间】:2020-05-17 23:21:28
【问题描述】:

我在将数据插入 mongoDb 的嵌套文档结构时遇到了一些问题。

以下是猫鼬模型:

const funnel = new mongoose.Schema({
funnelName:{
    type:String,
    unique:true
},
group: String,
category: String,
funnelStep: {
    stepType: String,
    stepName: String,
    stepPath: String,
    isTracking: Boolean,
    viewsStorage: []
} })

以下是我发送给 Db 的推送:

router.post('/createFunnel',async (req,res)=>{
if(!req.body.funnelName || !req.body.group || !req.body.category)
    {return res.status(422).json({error:"Please add all the fields."})}
try{
    const funnelSteps = []
    funnelSteps.push({
        stepType: req.body.stepType,
        stepName: req.body.stepName,
        stepPath: req.body.stepPath,
        isTracking: req.body.isTracking,
        viewsStorage: req.body.viewsStorage
    })
    const funnels = new Funnel({
        funnelName : req.body.funnelName,
        group : req.body.group,
        category : req.body.category,
        funnelStep : funnelSteps
    })
    await funnels.save(function(err){
        if(err){
            return res.status(422).send({error: err.message})
        }
        return res.json(funnels)
    })
} catch(err){
    return res.status(422).send({error: err.message})
} })

下面是我通过邮递员发送的数据结构:

{
"funnelName":"Name-Funnel",
"group":"AVC",
"category":"XYZ",
"funnelStep":[
    {
        "stepType":"Advert",
        "stepName":"Angle",
        "stepPath":"google.com",
        "isTracking":1,
        "viewsStorage":[0,0]
    },
    {
        "stepType":"Optin",
        "stepName":"Ver 1",
        "stepPath":"fb.com",
        "isTracking":1,
        "viewsStorage":[1,0]
    },
    {
        "stepType":"Check",
        "stepName":"rev-cat",
        "stepPath":"google.com",
        "isTracking":0,
        "viewsStorage":[2,0]
    }
] }

以下是我得到的响应输出:

{
    "funnelStep": {
        "viewsStorage": []
    },
    "_id": "5ec0ff78a6dfab18f4210e96",
    "funnelName": "Testing The Latest Method4",
    "group": "AVC",
    "category": "XYZ",
    "__v": 0
}

由于我的数据没有正确插入,我该如何解决这个问题?

除此之外,在viewsStorage数组中,如何存储日期和经过一定操作后会递增并根据日期保存到数组中的数字?

【问题讨论】:

    标签: node.js mongodb express postman mongoose-schema


    【解决方案1】:

    我认为funnelSteps 数组创建部分存在问题。您正在尝试直接从 req.body 而不是 req.body.funnelStep 获取数据

    const funnelSteps = []
    req.body.funnelStep.forEach(fs => {
        funnelSteps.push({
            stepType: fs.stepType,
            stepName: fs.stepName,
            stepPath: fs.stepPath,
            isTracking: fs.isTracking,
            viewsStorage: fs.viewsStorage
        })
    })
    

    架构

    const funnel = new mongoose.Schema({
    funnelName:{
        type:String,
        unique:true
    },
    group: String,
    category: String,
    funnelStep: [{
        stepType: String,
        stepName: String,
        stepPath: String,
        isTracking: Boolean,
        viewsStorage: []
    }] })
    

    【讨论】:

    • 嘿,感谢您的宝贵时间,但它仍然无法正常工作。我仍然得到相同的输出。
    • 我通过日志检查了 funnelSteps 数组中的数据是否正确填充,当 funnelsSteps 映射到漏斗时存在一些问题。
    • 漏斗模式有问题,漏斗步骤目前是一个JSON,需要做成任何数组。我已经更新了答案
    • 感谢 Vishnu 为它现在的工作所做的努力。我还有一个查询,我目前正在尝试在 viewsStorage 数组中存储日期并不断增加数字。例如 - [14/05/2020,255],[15/05/2020,150],[16/05/2020,200] 你能建议我实现上述数组的最佳方法吗?
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 2012-10-17
    • 2011-11-10
    • 1970-01-01
    • 2021-03-26
    • 2018-06-30
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多