ValidatorError添加logging

我正在试图添加一条logging到我的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); 

这是我的创build代码片段 –

 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到创build方法,也试过保存方法了。 两者都有同样的错误。 有什么我在这里失踪?

更新:
我试图validationlogging,然后将其添加到数据库,它给了同样的错误 –

 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"}}} 

我无法find用户名和密码的问题。 我想要这些字段作为必填字段的原因很明显。

我发现这个问题,我检查了是否正确获取data.username和data.password。 他们导致了undefined 。 然后我意识到数据实际上是一个string,而不是一个对象。 所以我把它parsing成json,然后让它工作。 这里有代码来做,以防有人陷入同样的​​问题 –

 data = JSON.parse(data);