保存数据期间出现MongoDB错误

时间:2017-04-01 07:35:26

标签: node.js mongodb

我对MongoDB有疑问。 我想保存MongoDB数据,但发生了一些(未指定的)错误。 我的服务器代码如下。

router.post('/send/:to', (req, res) => {
    let chatFrom = req.session.loginInfo.username;
    let chatTo = req.params.to;

    if((typeof chatFrom !== 'string') || (chatFrom === '')) {
        return res.status(400).json({
            error: 'NOT LOGGED IN',
            code: 1
        });
    } else if((typeof req.body.contents !== 'string') || (req.body.contents === '')) {
        return res.status(400).json({
            error: 'NO MESSAGE TO SEND',
            code: 2
        });
    }

    Account.findOne({username: chatFrom}, (err, data) => {
        if(err) {
            throw err;
        } else if(!data) {
            return res.status(400).json({
                error: 'INVALID SENDER NAME',
                code: 3
            });
        } else {
            if(data.chatList.indexOf(chatTo) === -1) {
                data.chatList.push(chatTo);
                data.save((err) => {
                    ////////////////////////////////////////////ERROR HERE
                    throw err;
                });
            }
        }
    });

    Account.findOne({username: chatTo}, (err, data) => {
        if(err) {
            throw err;
        } else if(!data) {
            return res.status(400).json({
                error: 'INVALID PARTNER NAME',
                code: 3
            });
        } else {
            if(data.chatList.indexOf(chatFrom) === -1) {
                data.chatList.push(chatFrom);
                data.save((err) => {
                    throw err;
                });
            }        

            let newChat = new Chat({
                from: chatFrom,
                to: chatTo,
                contents: req.body.contents,
                isRead: false,
            });
            newChat.save((err) => {
                if(err) {
                    throw err;
                } else {
                    return res.json({
                        success: true
                    });
                }
            });
        }
    });
});

错误发生在' //////// ERROR HERE'区域。 并且错误日志如下所示。

events.js:165       扔错了;       ^

Error: Uncaught, unspecified "error" event. (null)
    at Function.emit (events.js:163:17)
    at /home/jihwankim/Project/REACT/PurpleTalk/node_modules/mongoose/lib/model.js:3435:13
    at /home/jihwankim/Project/REACT/PurpleTalk/node_modules/mongoose/lib/services/model/applyHooks.js:144:20
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

这是node.js应用程序,而mongoose版本是4.7.7。

++++++++++++++++++++++++++ 我的模型如下所示

import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const Chat = new Schema({
    from: {
        type: String,
        trim: true
    },
    to: {
        type: String,
        trim: false
    },
    contents: {
        type: String,
        trim: true
    },
    isRead : {
        type: Boolean
    },
    sended: {
        type: Date,
        default: Date.now
    }
});

export default mongoose.model('chat', Chat);

0 个答案:

没有答案