架构路径'user.type'的值无效

时间:2018-06-05 23:27:49

标签: node.js api

我创建了一个帖子模型和一个后api以及对它的验证后出现错误。这是我所有的三个文件。错误是架构路径'user.type'的无效值但我不能我不明白这个错误在说什么。

 // to use a router we need to brinmg express there
const express=require('express');
const router=express.Router();
const mongoose = require('mongoose');
const passport = require('passport');
// Post model
const Post = require('../../models/Post');
// Profile model
const Profile = require('../../models/Profile');

// Validation
const validatePostInput = require('../../validation/post');

router.get('/test',(req,res)=>res.json({msg:"Posts Works"}));
module.exports=router;
// @route   POST api/posts
// @des create Post
// @access  Private
router.post('/',passport.authenticate('jwt',{session:false}),(req,res)=>{
  const{errors,isValid}=validatePostInput(req.body);
     if(!isValid){
         return res.status(400).json(errors);
     }
    const newPost=new POST({
      text:req.body.text,
      name:req.body.name,
      avatar:req.body.avatar,
      user:req.user.id
    });
newPost.save().then(post=>res.json(post));

});

这是帖子的模型

const mongoose=require('mongoose');
const Schema=mongoose.Schema;
const PostSchema=new Schema({
  user:{
    type:Schema.Types.OnjectId,
    ref:'users'
  },
  text:{
    type:String,
    required:true
  },
  name:{
    type:String
  },
  avatar:{
    type:String
  },
  likes:[
    {
      user:{
        type:Schema.Types.OnjectId,
        ref:'users'
      }
    }
  ],
  conmments:[
    {
      user:{
        type:Schema.Types.OnjectId,
        ref:'users'
      },
      text:{
        type:String,
        required:true
      },
      name:{
        type:String
      },
      avatar:{
        type:String
      },
      date:{
         type:Date,
         default:Date.now
      }

    }
  ],
  date:{
     type:Date,
     default:Date.now
  }
});
module.exports=Post=mongoose.model('post',PostSchema);

这是错误,它清楚地表明错误仅适用于post api,但我无法弄清楚这一点。

throw new TypeError('Invalid value for schema path `' + prefix + key + '`');
      ^

TypeError: Invalid value for schema path `user.type`
    at Schema.add (C:\Users\satyajeet\Desktop\socialnetwork-master\node_modules\mongoose\lib\schema.js:349:13)
    at Schema.add (C:\Users\satyajeet\Desktop\socialnetwork-master\node_modules\mongoose\lib\schema.js:362:14)
    at new Schema (C:\Users\satyajeet\Desktop\socialnetwork-master\node_modules\mongoose\lib\schema.js:93:10)
    at Object.<anonymous> (C:\Users\satyajeet\Desktop\socialnetwork-master\models\Post.js:3:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\satyajeet\Desktop\socialnetwork-master\routes\api\posts.js:7:14)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)

1 个答案:

答案 0 :(得分:3)

Schema.Types.OnjectId输入错误。它应该是Schema.Types.ObjectId。它贯穿整个模式定义。