提供所需值但猫鼬无法识别时如何解决猫鼬验证器错误

时间:2019-04-29 12:26:57

标签: node.js mongodb mongoose

我将文档保存到mongoDB,但是收到验证程序错误,提示 “ ValidatorError:路径properties.chargeBoxIdentity是必需的。”

我有一个运行良好的其他文档。我已经复制并粘贴并修改了它,但是此Heartbeat文档无法正常工作。

我尝试将chargeBoxIdentity值“硬编码”到文档中,但这没有用。 我在日期日期等格式中设置了一个硬编码的默认值,它确实起作用了,但这在我的用例中并不理想,因为每个充电器都有不同的标识。

const HeartbeatSchema = mongoose.Schema({
    properties: {
        time : { type : Date, default: Date.now },
        chargeBoxIdentity : { type: Number, required: true}
    },
    additionalProperties: false
})

module.exports = mongoose.model('Heartbeat', HeartbeatSchema)


// Here I set chargeBox Identity as 12345, but I get an error that properties.chargeBoxIdentity is required and won't save the document

const HB = new HeartbeatModel({
          chargeBoxIdentity: 12345

        });

  HB.save()
        .then(result => {console.log(result)})
        .catch(err => console.log(err));

1 个答案:

答案 0 :(得分:0)

这只是语法中的一个小错误,请将这些行更改为:

sink("Variance_model.txt")
cat("model {
# Priors
Y ~ dnorm(0,0.001)              #Mean richness
X ~ dnorm(0,0.001)              #Mean variance

for (a in 1:ncov){ # THIS IS THE ONLY LINE OF CODE THAT I MODIFIED
U[a] ~ dnorm(0,0.001)}      #Variance covariates

# Likelihood
for (i in 1:J) { 
mu[i] <- Y          #Hyper-parameter for station-specific all richness
NS[i] ~ dnorm(mu[i], tau[i])   #Likelihood
tau[i] <- (1/sigma2[i])
log(sigma2[i]) <- X + inprod(U,COV[i,])
}
}
", fill=TRUE)
sink()