简体   繁体   English

Dynamoose / DynamoDB更新将空数组保存为null

[英]Dynamoose/DynamoDB update saving empty array as null

I'm using the Node.js package Dynamoose to handle DynamoDB requests in my web application. 我正在使用Node.js包Dynamoose来处理Web应用程序中的DynamoDB请求。

Problem is when I try to update the item whenever the JSON object includes an empty array Dynamoose seems to set that to null. 问题是,当JSON对象包含空数组时,我尝试更新项目时,Dynamoose似乎将其设置为null。 Or it could be DynamoDB for all I know. 就我所知,它可能就是DynamoDB。

Below is part of my schema that I'm using for this table. 以下是我用于此表的部分架构。

var NoteSchema = new dynamoose.Schema({
    _id: String,
    details: Array
});

In the code below the variable body is set to {details: []} . 在下面的代码中,变量body设置为{details: []} I have confirmed this by running console.log(body); 我已经通过运行console.log(body);确认了这一点console.log(body); .

Note.update({
    _id: searchid
}, body, function(err, note) {
    if (err) {
        console.log(err);
    } else {
        console.log(note);
    }
});

Problem is inside that callback function when running console.log(note); 在运行console.log(note);时,问题在于回调函数内部console.log(note); details doesn't even show up at all. details甚至根本不显示。 So it's null or undefined. 因此它为null或未定义。 In the Amazon Web Services again details doesn't exist at all for that entry. 在Amazon Web Services中,该条目的details完全不存在。

What is so strange is when creating a new Note, setting details = [] , and saving that, details is an empty array and works perfectly. 令人奇怪的是,当创建一个新的Note时,将details = []设置details = []并将其保存,details是一个空数组,并且可以完美地工作。 So to me it seems like a specific problem with updating the record and setting that property to an empty array, since creating a record and setting that property to an empty array works perfectly. 因此对我来说,更新记录并将该属性设置为空数组似乎是一个特定的问题,因为创建记录并将该属性设置为空数组非常有效。

How can I update the record and set details to an empty array? 如何更新记录并将详细信息设置为空数组?

Figured this out. 想通了。 Submitted this issue to the GitHub repo. 将此问题提交到GitHub存储库。 Basically the following line ( lib/Model.js about line 396) of code checks to see if the value is an array with length = 0. If so it will delete that JSON key before sending it to AWS. 基本上,下面的代码行( 关于 396行的lib / Model.js )将检查该值是否是一个长度为0的数组。如果是这样,它将在将该JSON密钥发送给AWS之前将其删除。

if(val === null || val === undefined || val === '' || (Array.isArray(val) && val.length === 0)) {

I submitted a pull request using my fork . 我用叉子提交了拉取请求 In that pull request I made the change to allow an optional JSON object parameter of options to be passed into the update function. 在该拉取请求中,我进行了更改,以允许将选项的可选 JSON对象参数传递到更新函数中。 This is the 3rd parameter of the update function and right before the callback function. 这是更新函数的第三个参数,紧接在回调函数之前。 If there is a key called emptyarrayallowed and that value is set to true then you that line above will get changed into the following. 如果有一个名为emptyarrayallowed的键,并且该值设置为true则上面的那一行将变为以下内容。

if(val === null || val === undefined || val === '') {

For example in my example above changing the update function to the following will work (as long as you are using my fork or the pull request has been approved). 例如,在我上面的示例中,将更新功能更改为以下内容将起作用(只要您使用我的fork或pull请求已被批准)。

Note.update({_id: searchid}, body, {"emptyarrayallowed": true}, function(err, note) {
    if (err) {
        console.log(err);
    } else {
        console.log(note);
    }
});

Let me know if you have any questions regarding this solution. 如果您对此解决方案有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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