javascript - TypeError: Invalid value for schema path `type` - Stack Overflow

the error occurs while I try to run the code but for my colleague, it does not throw an error up to thi

the error occurs while I try to run the code but for my colleague, it does not throw an error up to this point

i tried to make syntax changes but did not workout.

var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/blog_demo_2", {useNewUrlParser:true});

//POST schema

var postSchema = new mongoose.Schema({
    title: String,
    content: String,
});

var Post = mongoose.model("Post", postSchema);

//USER schema

var userSchema = new mongoose.Schema({
    name: String,
    email: String,
    posts: [
            {
                 type: mongoose.Schema.Types.ObjectID,   
                 ref: "Post"
            }
         ]
});

var User = mongoose.model("User", userSchema);

wasif4000:~/workspace/associations (master) $ node references.js
/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414 throw new TypeError('Invalid value for schema path ' + prefix + key + ''); ^

TypeError: Invalid value for schema path `type`
    at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414:13)
    at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
    at Schema.interpretAsType (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:770:29)
    at Schema.path (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:596:27)
    at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:437:12)
    at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
    at Object.<anonymous> (/home/ubuntu/workspace/associations/references.js:16:18)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

the error occurs while I try to run the code but for my colleague, it does not throw an error up to this point

i tried to make syntax changes but did not workout.

var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/blog_demo_2", {useNewUrlParser:true});

//POST schema

var postSchema = new mongoose.Schema({
    title: String,
    content: String,
});

var Post = mongoose.model("Post", postSchema);

//USER schema

var userSchema = new mongoose.Schema({
    name: String,
    email: String,
    posts: [
            {
                 type: mongoose.Schema.Types.ObjectID,   
                 ref: "Post"
            }
         ]
});

var User = mongoose.model("User", userSchema);

wasif4000:~/workspace/associations (master) $ node references.js
/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414 throw new TypeError('Invalid value for schema path ' + prefix + key + ''); ^

TypeError: Invalid value for schema path `type`
    at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414:13)
    at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
    at Schema.interpretAsType (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:770:29)
    at Schema.path (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:596:27)
    at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:437:12)
    at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
    at Object.<anonymous> (/home/ubuntu/workspace/associations/references.js:16:18)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3
Share Improve this question asked Apr 23, 2019 at 11:07 wasif_Naquviwasif_Naquvi 691 silver badge5 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

the correction is

type: mongoose.Schema.Types.Object.Id,  

and not the below one where ID is uppercase, it should be lowercase

type: mongoose.Schema.Types.Object*ID*,

type: mongoose.Schema.Types.ObjectID - is not correct

it must be

type: mongoose.Schema.Types.ObjectId

Mongoose schema does not recognize ObjectID

-- from

mongoose.Schema.Types.ObjectID   

-- to

mongoose.Types.ObjectId

You have an extra ma in your code, look:

var postSchema = new mongoose.Schema({
    title: String,
    content: String,   ---->  You need to get rid of it
});

This should solve the problem.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745402175a4626151.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信