简体   繁体   English

TypeError:无法读取未定义的属性“json”

[英]TypeError: Cannot read property 'json' of undefined

Hello fellow developers.各位开发者您好。 I have a little problem here.我这里有个小问题。 Just started learning Node.js a couple of days ago.几天前刚开始学习 Node.js。 I build an API where i query some data from a MongoDB database.我构建了一个 API,在其中我从 MongoDB 数据库中查询一些数据。 The query itself looks like this:查询本身如下所示:

exports.getIndex = async (req, res) => {
    const marker = await Marker.find((data) => data);

    try {
        res.json(marker);
        console.log(marker)
    } catch (error) {
        console.log(error);
    }
};

I fire the query when i connect with a socket from my frontend.当我从前端连接套接字时,我触发查询。 That function looks like this: function 看起来像这样:

io.on("connection", (socket) => {
  adminController.getIndex();
  console.log("a user connected");
..etc
};

However, when my socket connects to the server i get:但是,当我的套接字连接到服务器时,我得到:

TypeError: Cannot read property 'json' of undefined at Object.exports.getIndex (.../backend/controllers/admin.js:8:13) TypeError:无法读取 Object.exports.getIndex 处未定义的属性“json”(.../backend/controllers/admin.js:8:13)

Anybody had a similar problem?有人有类似的问题吗? Any help would be much appreciated.任何帮助将非常感激。 Thanks.谢谢。 :) :)

So, as pointed out in the comment, you're using a function with 2 parameters: req and res .因此,正如评论中所指出的,您使用的是带有 2 个参数的 function: reqres
When calling the function adminController.getIndex() , you didn't passed any parameters where your function expect to get 2.调用 function adminController.getIndex()时,您没有传递 function 期望得到 2 的任何参数。
req and res are a naming convention for request and response in a http server (defined in the built in module http server and also in "Express" based http servers). reqres是 http 服务器中requestresponse的命名约定(在内置模块http服务器以及基于“Express”的 http 服务器中定义)。
As you're trying to trigger an API endpoint when you receive a connection from you socket.io connection, it appears that it should trigger an endpoint of your http server.当您收到来自socket.io连接的连接时,当您尝试触发 API 端点时,它似乎应该触发 http 服务器的端点。

Here's a way to do it:这是一种方法:

http.createServer(function (req, res) {
    // Put your socket.io connection event here
    // Pass req and res as parameters to your .getIndex(req, res) function
}).listen(9000);

Of course, there's tons of other ways to do that:D.当然,还有很多其他方法可以做到这一点:D。 This is just an example.这只是一个例子。 Another way would be to differentiate your http server from the socket.io one.另一种方法是将您的 http 服务器与 socket.io 服务器区分开来。 And in socket.io, create a http request with axios or request-promise and gather data from the result of the http request:). And in socket.io, create a http request with axios or request-promise and gather data from the result of the http request:).


http server: http 服务器:

http.createServer(function (req, res) {
    // Get data from DB and send it (your function)
}).listen(9000);

axios server: axios 服务器:

io.on("connection", async (socket) => {
  const response = await axios.get('localhost:9000/')
  // Deal with the response body sent by your server (res.json(data)) with the 
  // variable response.data
  console.log("a user connected");
..etc
};

Cheers !干杯!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM