简体   繁体   English

如何使用node.js从猫鼬模型获取数据并使用mvc之类的变量进行存储

[英]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.

相关问题 将来自 MongoDB 的数据存储在变量中 - mongoose 和 node.js - Store data from MongoDB in variable - mongoose and node.js 查询后使用mongoose和node.js存储变量数据 - Store variable data after query with mongoose and node.js 如何使用Node.JS和Mongoose将数据从MongoDB获取到简单数组? - How to get data from MongoDB to simple array using Node.JS and Mongoose? 如何从Mongoose模型node.js中删除对象 - How to remove an object from a Mongoose model node.js 无法使用mongoose从node.js上mongodb的新集合中获取数据 - Unable to get data from new collection on mongodb on node.js using mongoose 如何使用Node.js从Node.js发送和获取JSON数据到HTML - How to send and get JSON data from Node.js into HTML using Node.js Node.js - 从域模型中提取猫鼬模型 - Node.js - Abstracting mongoose model from domain model 如何从数据库导入数据并将其存储在 node.js 应用程序中的变量中以供以后使用 - How to import data from a database and store it in a variable in a node.js app for later use Node.JS和Mongoose OOP-如何从纯数据对象中分离出Mongoose数据访问代码? - Node.JS with Mongoose OOP - how to separate mongoose data access code from pure data object? Node.js + Mongoose:模型/模式与db逻辑分离 - Node.js + Mongoose: Model / Schema separated from db logic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM