如何在猫鼬中包含外部模型?

时间:2021-08-21 02:30:47

i have a problem when i want to include my models with mongoose in nodejs, i'm create a models with schema like this

我有一个问题,当我想在nodejs中包含mongoose的模型时,我创建了一个模式,这样的模式

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var Users = new Schema({
    idUser  : {type:String},
    username: {type:String}
});

// middleware
Users.pre('save', function (next,done) {
  notify(this.get('email') + done);
  // something goes wrong
  next(new Error('something went wrong'));
});

//registered on mongoose models
mongoose.model("Users",Users);

and i save in folde models/schema.js but i don't know how to call that file in app.js , when i'm try using this code

我保存在folde models / schema.js但我不知道如何在app.js中调用该文件,当我尝试使用此代码时

var mongoose = require('mongoose')
  , models = require('./models/schema.js');

//mongoose configurationfor database;
var db = mongoose.connect("mongodb://localhost/vynchat");
var users = mongoose.model("Users");
users.save();

i have error when i try to start sudo node app.js

我尝试启动sudo节点app.js时出错

throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Object function model() {
    Model.apply(this, arguments);
  } has no method 'save'
    at Object.<anonymous> (/opt/development/vynapp/app.js:18:7)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)

the error model() has no method save... how i can fix this..?

错误模型()没有方法保存...我怎么能解决这个问题?

2 个解决方案

#1


6  

You are calling users.save();, but, users is a Model.

您正在调用users.save();,但是,users是一个Model。

The save methods can be used on instances of models, something like:

保存方法可用于模型实例,例如:

var mongoose = require('mongoose')
  , models = require('./models/schema');

var db = mongoose.connect("mongodb://localhost/vynchat")
  , Users = mongoose.model("Users");


var user = new User({
      "your": "data"
    });

user.save(function (err, model) {
  if (err) throw err;

  console.log("My new User is saved",
    "`save` hook worked as espected since we had no errors here");
});

You should read the node.js modules API and carefully read mongoose API.

您应该阅读node.js模块API并仔细阅读mongoose API。

As a side note: when you require('mongoose') the first time in your code node will give you an instance of the mongoose connector, subsequent requires will yield the same object.

作为旁注:当您需要('mongoose')时,代码节点中的第一次将为您提供mongoose连接器的实例,后续需求将产生相同的对象。

#2


0  

You are missing schema object param while creating model object. Do this:

在创建模型对象时,您缺少模式对象参数。做这个:

var users_model = mongoose.model("Users", YourSchemaObject);
var User_Document = new user_model();
User_Document.field_name = 'value'; //here you can access getter/setter/methods
User_Document.save();

#1


6  

You are calling users.save();, but, users is a Model.

您正在调用users.save();,但是,users是一个Model。

The save methods can be used on instances of models, something like:

保存方法可用于模型实例,例如:

var mongoose = require('mongoose')
  , models = require('./models/schema');

var db = mongoose.connect("mongodb://localhost/vynchat")
  , Users = mongoose.model("Users");


var user = new User({
      "your": "data"
    });

user.save(function (err, model) {
  if (err) throw err;

  console.log("My new User is saved",
    "`save` hook worked as espected since we had no errors here");
});

You should read the node.js modules API and carefully read mongoose API.

您应该阅读node.js模块API并仔细阅读mongoose API。

As a side note: when you require('mongoose') the first time in your code node will give you an instance of the mongoose connector, subsequent requires will yield the same object.

作为旁注:当您需要('mongoose')时,代码节点中的第一次将为您提供mongoose连接器的实例,后续需求将产生相同的对象。

#2


0  

You are missing schema object param while creating model object. Do this:

在创建模型对象时,您缺少模式对象参数。做这个:

var users_model = mongoose.model("Users", YourSchemaObject);
var User_Document = new user_model();
User_Document.field_name = 'value'; //here you can access getter/setter/methods
User_Document.save();