我如何使用nodejs和dynamodb将一个数组项附加到另一个数组?

时间:2019-03-08 06:56:45

标签: node.js amazon-dynamodb

我正在使用JSON结构存储博客文章的评论和答复。基本上,id,storeId,blogUrl,blogTitle等将是其他属性,主键将是blogId。

发布回复后,我需要将回复推送到评论数组。

JSON结构-

"blogStatus": "published",
        "blogContent": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.,
        "storeId": "10",
        "tagDate": "Thu Mar 07 2019 18:02:33 GMT+0530 (India Standard Time)",
        "type": "comment",
        "blogTitle": "best hospitalsss",
        "createdDate": "Thu Mar 07 2019 18:02:33 GMT+0530 (India Standard Time)",
        "Comments": [
            {
                "date": "Fri Mar 08 2019 10:24:50 GMT+0530 (India Standard Time)",
                "userEmail": "liza@gmail.com",
                "id": "4e4e8210-415e-11e9-bd72-a17467608922",
                "userName": "liza",
                "status": "draft",
                 "replies": [
                {
                    "date": "Fri Mar 08 2019 09:54:00 GMT+0530 (India Standard Time)",
                    "userEmail": "liza1@gmail.com",
                    "id": "ff3096e0-4159-11e9-9315-652881dffd8b",
                    "userName": "liza1",
                    "status": "draft"
                },
              {
                    "date": "Fri Mar 08 2019 09:54:00 GMT+0530 (India Standard Time)",
                    "userEmail": "liza2@gmail.com",
                    "id": "ff3096e0-4159-11e9-9315-652881dffd8b",
                    "userName": "liza2",
                    "status": "draft"
                }
              }
            ]

        ]

这是我想要的结构。.首先,我创建一个带有空评论和回复数组的博客文章。.我使用dynamodb中的list_append将评论附加到博客文章中。.我需要在评论中插入多个回复数组数组作为内部数组。

这是我的API,用于将回复推送到评论数组。.但是对于每个api命中,回复项目将替换为评论数组,而我需要添加新项目

app.post('/api/v1/reply/:id', function (req, res) {
    var blogId = req.params.id;
    var replyId = uuid.v1();
    var d = new Date();
    var date = d.toString();
    const {
        reply,
        userName,
        userEmail,
        status
    } = req.body
    var params = {
    TableName: "BlogTest",

     Key: {
     "id": blogId,
     },
     UpdateExpression: "SET #comments[0].#replies = :attrValue",
     ExpressionAttributeNames: {
         '#comments': 'Comments',
         '#replies': 'replies'
     },
   "ExpressionAttributeValues": {
     ":attrValue": [{ id: replyId, date: date, message:reply, userName:userName, userEmail:userEmail,status:status},{ id: replyId, date: date, message:reply, userName:userName, userEmail:userEmail,status:status}]
   },
   ReturnValues: "UPDATED_NEW"
 };
console.log(params);

 docClient.update(params, (error) => {
    if (error) {
        console.log(error);
        res.status(400).json({ error: 'Could not create reply' });
    }
    res.status(200).json({ success: 'Created the reply' });
    // res.json({ id, name, info });
});
})

任何人都请帮助。。谢谢

0 个答案:

没有答案
相关问题