猫鼬给我未定义的错误

时间:2015-08-07 04:55:35

标签: node.js database mongodb mongoose

好的,我没有得到这一点。它实际上工作不久前,但现在我收到以下错误:

错误

azura@AzuraMain:~$ nodejs /home/azura/Desktop/dbWrite.js
Connection to database has been established
Server is up

/home/azura/Desktop/dbWrite.js:94
    res.send("<h1>Hello</h1> " + id + " " + data.name);
                                                ^
TypeError: Cannot read property 'name' of undefined
at /home/azura/Desktop/dbWrite.js:94:53
at /home/azura/node_modules/mongoose/node_modules/kareem/index.js:160:11
at Query._findOne (/home/azura/node_modules/mongoose/lib/query.js:1145:12)
at /home/azura/node_modules/mongoose/node_modules/kareem/index.js:156:8
at /home/azura/node_modules/mongoose/node_modules/kareem/index.js:18:7
at process._tickCallback (node.js:415:13)

我不明白为什么我会收到这个错误。

这是我的代码:

服务器代码

var mongoose = require("mongoose");
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
//Use These Modules



app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");

});
//Create the homepage of the server



mongoose.connect("mongodb://localhost:27017/NEW_DB1");
console.log("Connection to database has been established");
//Connect to the database



var collectedData = new mongoose.Schema({
    ipAddress: String,
    name: {
        type: String,
        unique: false
    }
});
var collectionOfData = mongoose.model("dataType", collectedData);
//Create the mongoose schema

io.on("connection", function (socket) {
//Check for connection with socket.io



    socket.on("name", function (e) {
    //Check for "name" with socket.io



        var ip = socket.request.socket.remoteAddress;
        //Check the ip address of user



        var dataBase = mongoose.connection;
        var Maindata = new collectionOfData({
            ipAddress: ip,
            name: e
        });
        //Create the Schema with the requested name and ip



        Maindata.save(function (err, Maindata) {
            if (err) {
                return console.error(err);
            } else {
                console.dir(Maindata);

            }
        });
        //Save this into the database




    });
});




app.get("/mix", function (req, res) {
    collectionOfData.find(function (err, data) {
        res.send(data);
    });
});
//Just a test directory /mix

app.get("/:uniqueURL", function (req, res) {
    var id = req.params.uniqueURL;
//Create a unique URL

    collectionOfData.findOne({
        _id: id

    }, function (err, data) {
        res.send("<h1>Hello</h1> " + id + " " + data.name);
        //This is where the issue derives from data.name is undefined? I Defined it up there and it seems to work for a second until the server crashed because of it

    });
    //Send the data to the requested page
});


http.listen(10203, function () {
    console.log("Server is up");
});
//Create the HTTP Server

HTML CODE

<html>

    <body>
        <form id="chooseName">
            <input class="center-block" id="name" placeholder="Post whatever the fuck you want" />
        </form>
        <script src="/socket.io/socket.io.js">
        </script>
        <script src="http://code.jquery.com/jquery-1.11.1.js">  </script>
        <script>
            var socket = io();

            $("#chooseName").submit(function (e) {
                e.preventDefault();
                socket.emit("name", $("#name").val());
                document.write("cool go to http://173.78.185.247:10203/mix to see what you have contributed to");
    });
    //Send data to the server where it gets read with socket.on("name", Do Something
        </script>
     </body>
  </html>

为什么会这样?我想要的是使data.name打印出所请求的用户名。它似乎工作了一秒钟,但服务器崩溃了。

1 个答案:

答案 0 :(得分:0)

如果集合数据中没有匹配的文档,则findOne()可以在数据对象中提供null。

通过打印id,err和data进行调试。