Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 架构路径';的值无效;user.type';_Node.js_Api - Fatal编程技术网

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

Node.js 架构路径';的值无效;user.type';,node.js,api,Node.js,Api,在创建post模型和post 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 pas

在创建post模型和post 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);
这是一个错误,它清楚地表明错误仅与PostAPI有关,但我无法理解这一点

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)
抛出新的TypeError('schema path`+prefix+key+'`的值无效');
^
TypeError:架构路径'user.type'的值无效`
在Schema.add(C:\Users\satyajeet\Desktop\socialnetwork master\node\u modules\mongoose\lib\Schema.js:349:13)
在Schema.add(C:\Users\satyajeet\Desktop\socialnetwork master\node\u modules\mongoose\lib\Schema.js:362:14)
在新模式下(C:\Users\satyajeet\Desktop\socialnetwork master\node\u modules\mongoose\lib\Schema.js:93:10)
反对。(C:\Users\satyajeet\Desktop\socialnetworkmaster\models\Post.js:3:18)
编译(Module.js:652:30)
在Object.Module.\u extensions..js(Module.js:663:10)
在Module.load(Module.js:565:32)
在tryModuleLoad时(module.js:505:12)
在Function.Module.\u加载(Module.js:497:3)
at Module.require(Module.js:596:17)
根据需要(内部/module.js:11:18)
反对。(C:\Users\satyajeet\Desktop\socialnetworkmaster\routes\api\posts.js:7:14)
编译(Module.js:652:30)
在Object.Module.\u extensions..js(Module.js:663:10)
在Module.load(Module.js:565:32)
在tryModuleLoad时(module.js:505:12)

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

我更正了它,但仍然收到相同的错误消息,哦,是的,我在模型中的所有位置都更正了它,它解决了问题。没问题。每个人都会这样。将问题标记为已回答以关闭它:D