简体   繁体   English

如何将JSON对象从控制器传递到路由器?

[英]How to pass json object from controller to router?

I have a data in controller from mongodb now i want to send this json object to router so i can send to client side using api , with below code i am getting an error TypeError: Cannot read property 'json' of undefined Any idea what is implemented wrong ? 我现在有一个来自mongodb的控制器中的数据,我想将此json对象发送到路由器,以便我可以使用api发送到客户端,使用以下代码,我收到一个错误TypeError: Cannot read property 'json' of undefined任何想法实行错了吗?

controller.js controller.js

var Diagram = require('./diagram.model');
var mongoose = require('mongoose');

module.exports = function index(req,res) {
       Diagram.find({}, function(err, result) {
         if (!err) {
           console.log('Response from controller', result);
           return res.json(result);
         }
       });
     }

router.js router.js

var express = require('express');
var controller = require('./diagram.controller');
var router = express.Router();
console.log('THis is in router',controller.index);
router.get('/getAllDiagram',controller.index);
module.exports = router;

I think the module.exports (see my comment above) is the problem. 我认为module.exports (请参阅上面的评论)是问题所在。 What do you think about writing your request handling straightforward first (so that you have a feeling of success (: ): 您对首先编写直接处理的请求有什么看法(以便您有成功的感觉(:):

const express = require('express');
const app = express();

app.get('/getAllDiagram', (req, res) => {
     Diagram.find({}, function(err, result) {
         if (err) {
             console.error(`Error in finding diagram: ${err.message}`);

             return res.status(500);
         }

         res.json(result);
     });
});

app.listen(8080);

Advanced version 进阶版

controller.js controller.js

const Diagram = require('./diagram.model');

module.exports.index = (req, res) => {
    Diagram.find({}, function(err, result) {
        if (err) {
            console.error(`Error in finding diagram: ${err.message}`);

            return res.status(500);
        }

        res.json(result);
    });
};

router.js router.js

const express = require('express');

const controller = require('./controller');
const router = express.Router();

router.get('/getAllDiagram', controller.index);

module.exports = router;

app.js app.js

const express = require('express');

const router = require('./router');

const app = express();

app.use(router);

app.listen(8080);

Important: Please check the module.exports.index declaration. 重要:请检查module.exports.index声明。 That was wrong in your code snippet. 这在您的代码片段中是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 node.js 中将数据从控制器传递到路由器? - How to pass data from controller to router in node.js? 如何将从控制器返回的数据传递到Express'路由器? - How do I pass data returned from a controller to Express' router? 如何使用 NodeJS 将 jwt 令牌从控制器传递到路由器 - How to pass jwt token from controller to router using NodeJS 如何将JSON数据从控制器传递到JavaScript - How to pass json data from the controller to javascript 如何将对象从视图传递到控制器 - how to pass object from view to controller 如何将对象从angularjs传递到spring控制器 - How to pass an object from angularjs to spring controller javascript-如何将JSON对象中的数组从JavaScript传递给asp.net MVC控制器方法? - How to pass array in a JSON object from javascript to asp.net mvc controller method? 如何从写在另一个视图上的JS代码重定向到控制器,并与之一起传递JSON对象 - How to redirect to a controller from JS code written on another view and pass JSON object along with it 如何从ASP.NET MVC中的控制器传递JSON中的chart.js图表​​数据对象 - How to pass a chart.js chart data object in json from a controller in asp.net mvc asp.net mvc将Json对象从视图传递到控制器 - asp.net mvc pass Json object from view to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM