UnhandledPromiseRejectionWarning:ValidationError:注释验证失败强制转换为ObjectID的值

时间:2019-02-25 10:43:56

标签: node.js mongoose socket.io

我正在使用Socket.io,Mongodb和Nodejs开发实时评论系统,我想在数据库中保存用户ID和发布ID,我目前正在使用Jquery从前端登录用户ID,但是当我尝试保存时用户它给出以下错误。 (节点:16632)UnhandledPromiseRejectionWarning:ValidationError:注释验证失败:用户:在路径“ user”处,值“”的强制转换为ObjectID失败

我的评论架构为

let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let CommentSchema =  Schema({
body:{
    type:String,
    required:true,
},
date:{
    type:Date,
    default:new Date().toLocaleDateString()
},
user: {

    type: Schema.Types.ObjectId,
    ref: 'users'


},

}); module.exports = mongoose.model('comments',CommentSchema);

这是我的Socket.io代码。

module.exports = function(io) {
const express = require('express');
let router = express.Router()
let Comment = require('../../models/Comments');
let Post = require('../../models/Post');
// define routes
// io is available in this scope

io.on('connection',function(socket){
    socket.on('comment',function(data){

Post.findOne({_id:data.postId}).then(post=>{
            console.log(data.user);
           var newComment = new Comment({  
                body:data.comment,
                user:data.user,                  
            });
                    post.comments.push(newComment);
                    post.save().then(postSaved=>{
                    newComment.save().then(commentSaved=>{
                    socket.broadcast.emit('comment',data);
                    console.log('Comment saved');
                    console.log(data.user);  
             });
            });
        });


        // console.log(data.postId);
    });
});


return router;

}

我的Jquery代码是

`<script type="text/javascript">
var socket = io();
   var userFname = $('#useronlinefname').val();
   var postID = '';
   var commentS = '';

$(document).ready(function(){



$(".send").click(function(e){
   e.preventDefault(); // prevent default form redirect
   var ThisForm = $(this).closest('.commentForm');   // get this parent form
   var Id = ThisForm.find('input.postId').val();     // get postId
   var userId = $('#useronlineid').val();
   var userFname = $('#useronlinefname').val();
   var commentData = ThisForm.parents('.Commentl').children('.commentData');
   var Comment = ThisForm.find('input[type="text"].comment').val();  // get comment
   postID = Id;
   commentS = commentData;



   console.log(Id +' ||| '+ Comment);




   if(Comment != ''){


     commentData.append("<div class='media mb-4 showcomment'><img class='d-flex mr-3 rounded-circle' src='/uploads/{{user.profilePic}}' alt=''><div class='media-body'><b class='mt-0'>"+userFname+':'+' '+"</b><span>"+Comment+"</span></div></div>");


     $('.comment').val('');



   }

   socket.emit('comment',{comment:Comment,postId:Id,user:userId});



});
})

socket.on('comment',function(data){
    if(postID == data.postId){
      $('.commentData1').append("<div class='media mb-4 showcomment'><img class='d-flex mr-3 rounded-circle' src='/uploads/{{user.profilePic}}' alt=''><div class='media-body'><b class='mt-0'>"+userFname+':'+' '+"</b><span>"+data.comment+"</span></div></div>");
    }
});



</script>

`

我想在注释数据库中保存用户ID

0 个答案:

没有答案