Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 保存mongoose数组架构类型时忽略null或空值_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 保存mongoose数组架构类型时忽略null或空值

Node.js 保存mongoose数组架构类型时忽略null或空值,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有以下模式: const userSchema = new mongoose.Schema({ email: [{ type: String, trim: true, }] }) 保存新用户时 const user = new User({ email: ["example@example.com", ""] //or email: ["example@example.com", null] }) try{ await

我有以下模式:

const userSchema = new mongoose.Schema({
    email: [{
        type: String,
        trim: true,
     }]
})

保存新用户时

const user = new User({
    email: ["example@example.com", ""]
    //or email: ["example@example.com", null]
})

try{
   await user.save()
} catch (e) {
   console.log(e)
}

这将保存这两个值(分别包括空字符串和null)


是否有一种方法可以在丢弃空值或空值的同时只保存正确的电子邮件值。 与此相反:

"email" : [ 
        "example@example.com", 
        ""
    ],

仅存储正确的电子邮件:

"email" : [ 
        "example@example.com", 
    ],

目前,对于其他模式字段,我使用set。例如,在上面的用户模式中

url: {
    type: String,
    set: deleteEmpty
}



const deleteEmpty = (v) => {
  if(!v) {
      return undefined
  }

  return v
}

如果该值为空或null,则这当然不会保存url字段。 但是,在上面的电子邮件字段上使用此方法将生成空值


是否有办法只存储正确的电子邮件值(即“example@example.com“在这种情况下,忽略null或空值。)?

您可以执行以下操作以实现所需

var arr=['example@example.com', '']; // 变量来保持数组中包含的值
var i;
对于(i=0;i

祝你好运,我希望我回答了你的问题。

你可以做以下事情来实现你想要的

var arr=['example@example.com', '']; // 变量来保持数组中包含的值
var i;
对于(i=0;i

祝你好运,我希望我回答了你的问题。

如果有人有一个或多个接受数组值的字段并希望检查每个字段,我建议在预保存挂钩上使用中间件

supplierSchema.pre('save', normalizeArray)


const normalizeArrray = function(next) {

    //take the list of object feilds and for each field check if it is array
    Object.keys(this.toObject()).forEach((field) => {

        if(Array.isArray(this[field])) {

            //removes null or empty values
            this[field] = this[field].filter(field => field)
        }
    })

    next()
}

这只是基于上面已经批准的答案。

如果任何人有一个或多个字段接受数组值,并且希望检查每个字段,我建议在预保存挂钩上使用中间件

supplierSchema.pre('save', normalizeArray)


const normalizeArrray = function(next) {

    //take the list of object feilds and for each field check if it is array
    Object.keys(this.toObject()).forEach((field) => {

        if(Array.isArray(this[field])) {

            //removes null or empty values
            this[field] = this[field].filter(field => field)
        }
    })

    next()
}

这只是基于上面已经批准的答案。

只需将要忽略的字段的默认值(如果为空)设置为
未定义。另外,将
required
设置为
false


使用以下代码:

const userSchema = new mongoose.Schema({
    email: [{
        type: String,
        trim: true,
        required: false,
        default: undefined
     }]
})

只需将要忽略的字段的默认值(如果为空)设置为
未定义
。另外,将
required
设置为
false


使用以下代码:

const userSchema = new mongoose.Schema({
    email: [{
        type: String,
        trim: true,
        required: false,
        default: undefined
     }]
})

为什么不干脆
this.email=this.email?this.email:未定义为什么不干脆
this.email=this.email?this.email:未定义