gpt4 book ai didi

javascript - 添加记录时出现 ValidatorError

转载 作者:行者123 更新时间:2023-11-30 16:45:26 25 4
gpt4 key购买 nike

我正在尝试向我的 mongodb 数据库添加一条记录。我确信我提供了正确的值,尽管 mongoose 认为它们丢失了。这是我的架构 -

var mongoose = require('mongoose');

var SocketUserSchema = new mongoose.Schema({
name: String,
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
devices: [{
name: String,
platform: String
}],
first_ip: {
type: String,
default: "0.0.0.0"
},
last_login_ip: {
type: String,
default: "0.0.0.0"
},
added_at: {
type: Date,
default: Date.now
},
});

module.exports = mongoose.model('SocketUser', SocketUserSchema);

这是我的创建代码片段 -

console.log('registered triggered ' + data);
/*var jsonData = data;
jsonData.first_ip = clientIpAddress;
jsonData.last_login_ip = clientIpAddress;*/
var user = new SocketUser({
username: data.username,
password: data.password,
devices: data.devices,
first_ip: clientIpAddress,
last_login_ip: clientIpAddress
});

SocketUser.create(user, function(err, post) {
if (err) {
console.log('error',err);
} else {
console.log('success');
}
});

这是我发送的示例用户数据 -

{
"username": "nexus",
"password": "noob",
"devices": [
{
"name": "Nexus",
"platform": "android"
}
]
}

这是我得到的错误 -

{
"message": "SocketUser validation failed",
"name": "ValidationError",
"errors": {
"username": {
"properties": {
"type": "required",
"message": "Path `{PATH}` is required.",
"path": "username"
},
"message": "Path `username` is required.",
"name": "ValidatorError",
"kind": "required",
"path": "username"
},
"password": {
"properties": {
"type": "required",
"message": "Path `{PATH}` is required.",
"path": "password"
},
"message": "Path `password` is required.",
"name": "ValidatorError",
"kind": "required",
"path": "password"
}
}
}

如您所见,用户名和密码这两个字段都在数据中,但它仍然给出错误。我试过将数据 json 直接发送到 Create 方法,也尝试过保存方法。两者都给出相同的错误。我在这里遗漏了什么吗?

更新:
在将记录添加到数据库之前,我尝试验证记录,但它给出了同样的错误 -

user.validate(function(err) {
if (err) {
console.log("error "+err.message+"\nFull error: "+JSON.stringify(err));
}
else {
}
});

registered triggered {"username":"nexus","password":"noob","devices":[{"name":"N
exus","platform":"android"}]}
error SocketUser validation failed
Full error: {"message":"SocketUser validation failed","name":"ValidationError","
errors":{"password":{"properties":{"type":"required","message":"Path `{PATH}` is
required.","path":"password"},"message":"Path `password` is required.","name":"
ValidatorError","kind":"required","path":"password"},"username":{"properties":{"
type":"required","message":"Path `{PATH}` is required.","path":"username"},"mess
age":"Path `username` is required.","name":"ValidatorError","kind":"required","p
ath":"username"}}}

我找不到用户名和密码的问题。出于显而易见的原因,我希望将这些字段作为必填字段。

最佳答案

我发现了问题,我检查了我是否正确获取了 data.username 和 data.password。它们导致 undefined。然后我意识到数据实际上是一个字符串,而不是一个对象。所以我把它解析成 json 然后让它工作。这是执行此操作的代码,以防有人遇到同样的问题 -

data = JSON.parse(data);

关于javascript - 添加记录时出现 ValidatorError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31315488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com