[英]How to get data from mongoose model and store with variable like mvc using node.js
Here below code is sample MVC framework code in PHP. 下面的代码是PHP中的示例MVC框架代码。 I need same process as like in node.js with mongoose also. 我也需要像在node.js中一样使用mongoose的过程。
I'm using Node.js, MongoDB, REST API development. 我正在使用Node.js,MongoDB,REST API开发。
controller file: 控制器文件:
<?php
class Myclass {
public function store_users() {
//get the data from model file
$country = $this->country->get_country_details($country_id);
//After getting data do business logic
}
}
model file 模型文件
<?php
class Mymodel {
public function get_country_details($cid) {
$details = $this->db->table('country')->where('country_id',$id);
return $details;
}
}
In node.js need to use as like MVC PHP process. 在node.js中需要使用像MVC这样的PHP程序。 Kindly suggest on this. 请对此提出建议。
Lets say you have user schema in mongoose which should act as model 假设您在猫鼬中有用户模式,应该作为模型
// userModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user = new Schema({
name: { type: String, required: true },
dob: { type: Date },
email: { type: String, required: true, unique: true, lowercase: true},
active: { type: Boolean, required: true, default: true}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
var userSchema = mongoose.model('users', user);
module.exports = userSchema;
// userController.js
var User = require('./userModel');
exports.getUserByEmail = function(req, res, next) {
var email = req.param.email;
User.findOne({ email: email }, function(err, data) {
if (err) {
next.ifError(err);
}
res.send({
status: true,
data: data
});
return next();
});
};
Ref: http://mongoosejs.com/docs/index.html 参考: http : //mongoosejs.com/docs/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.