Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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 mongoose-blogPost不是构造函数错误(架构/模型错误)_Node.js_Mongoose_Schema - Fatal编程技术网

Node.js mongoose-blogPost不是构造函数错误(架构/模型错误)

Node.js mongoose-blogPost不是构造函数错误(架构/模型错误),node.js,mongoose,schema,Node.js,Mongoose,Schema,我是mongoose和mongodb的新手,我正在尝试连接到mlab并插入一些数据,但我一直收到以下代码错误 var newPost = new blogPost({title:'First Post', post:'this is my first post'}); ^ TypeError: blogPost is not a constructor at Object.<anonymous> (C:\Users\ipingou\website

我是mongoose和mongodb的新手,我正在尝试连接到mlab并插入一些数据,但我一直收到以下代码错误

var newPost = new blogPost({title:'First Post', post:'this is my first post'});
              ^

TypeError: blogPost is not a constructor
    at Object.<anonymous> (C:\Users\ipingou\website\hwnay\index2.js:39:15)
    at Module._compile (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:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

你错过了一个关键的部分。因此,您已经定义了一个模式,但需要从该模式创建一个模型:

var Post = mongoose.model('blogPost', blogPost);

var newPost = new Post({ title: 'First Post', post: 'this is my first post' });

newPost.save(function(err, newPost){
    if(err) {
      return console.log(err);
    }
    console.log(newPost);
});

你可以找到一个例子。可以找到更详细的解释。

谢谢!就这样。我想我应该更仔细地阅读文档。thx~!听到这个消息我很高兴。你能接受我的回答作为你问题的答案吗?
var Post = mongoose.model('blogPost', blogPost);

var newPost = new Post({ title: 'First Post', post: 'this is my first post' });

newPost.save(function(err, newPost){
    if(err) {
      return console.log(err);
    }
    console.log(newPost);
});