Javascript 添加记录时出错
我正在尝试向我的mongodb数据库添加一条记录。我确信我提供了正确的值,尽管mongoose认为它们缺失了。这是我的模式-Javascript 添加记录时出错,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在尝试向我的mongodb数据库添加一条记录。我确信我提供了正确的值,尽管mongoose认为它们缺失了。这是我的模式- var mongoose = require('mongoose'); var SocketUserSchema = new mongoose.Schema({ name: String, username: { type: String, unique: true,
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"
}
}
}
如您所见,username和password这两个字段都在数据中,但它仍然给出了错误。我尝试过直接发送json数据来创建方法,也尝试过保存方法。两者都给出相同的错误。这里有我遗漏的东西吗
更新:在将记录添加到数据库之前,我尝试验证该记录,但它给出了相同的错误-
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。它们导致
未定义。然后我意识到数据实际上是一个字符串,而不是一个对象。所以我把它解析成json,然后让它工作起来。下面是代码,以防有人遇到同样的问题-
data = JSON.parse(data);