Postman 在我的 NodeJS MongoDB 应用程序上显示无限的“正在发送请求...”

问题描述 投票:0回答:1

这是我的聊天机器人应用程序的完整代码

const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const bodyParser = require('body-parser'); 
const dbName = "chatbotdb";

const jsonParser = bodyParser.json();
const urlencodedParser = bodyParser.urlencoded({extended: false});

app.use(jsonParser);
app.use(urlencodedParser);

const MongoConnection = (callback) =>{

    //creating connection to MongoDB database with connect() method which returns a promise
    MongoClient.connect('mongodb://localhost:27017/chatbotdb') //pass the DB along with connection-URL
    .then(client=>{
        console.log("Connected");
        db = client.db();  //fetch DB
        callback();
     })
    .catch(err=>{
        console.log(err);
     });
}



app.post('/insert', urlencodedParser, function(req, res){
    let objJson = {};
    if(req.body.code_user) objJson.code_user = req.body.code_user; else objJson.code_user = 0;
    if(req.body.code_session) objJson.code_session = req.body.code_session; else objJson.code_session = 0;
    if(req.body.code_current) objJson.code_current = req.body.code_current; else objJson.code_current = cod();
    if(req.body.code_relation) objJson.code_relation = req.body.code_relation; else objJson.code_relation = 0;
    if(req.body.code_before) objJson.code_before = req.body.code_before; else objJson.code_before = 0;
    if(req.body.input) objJson.input = req.body.input; else objJson.input = '';
    if(req.body.output) objJson.output = req.body.output; else objJson.output ='Desculpe, mas não entendi';

    insertData(objJson, function(result){
        res.send(result);
    })
});

function cod(){
    const data = new Date();
    const ano = data.getFullYear();
    const mes = data.getMonth();
    const dia = data.getDate();
    const hora = data.getHours();
    const minuto = data.getMinutes();
    const segundo = data.getSeconds();
    const milisegundos = data.getMilliseconds();
    const result = parseInt(Number(ano+''+mes+''+dia+''+hora+''+minuto+''+segundo+''+milisegundos)/2);
    return result;
}

app.post('/update', urlencodedParser, function(req, res){
    let objJson = {};
    if(req.body.code_user) objJson.code_user = req.body.code_user;
    if(req.body.code_session) objJson.code_session = req.body.code_session; 
    if(req.body.code_current) objJson.code_current = req.body.code_current;
    if(req.body.code_relation) objJson.code_relation = req.body.code_relation;
    if(req.body.code_before) objJson.code_before = req.body.code_before; 
    if(req.body.input) objJson.input = req.body.input; 
    if(req.body.output) objJson.output = req.body.output; 

    updateData(objJson, function(result){
        res.send(result);
    })
});

app.post('/delete', urlencodedParser, function(req, res){
    let objJson = {};
    if(req.body.code_user) objJson.code_user = req.body.code_user; 
    if(req.body.code_session) objJson.code_session = req.body.code_session; 
    if(req.body.code_current) objJson.code_current = req.body.code_current;
    if(req.body.code_relation) objJson.code_relation = req.body.code_relation;
    if(req.body.code_before) objJson.code_before = req.body.code_before; 
    if(req.body.input) objJson.input = req.body.input; 
    if(req.body.output) objJson.output = req.body.output; 

    deleteData(objJson, function(result){
        res.send(result);
    });
    });

    app.post('/find', urlencodedParser, function(req, res){
        let objJson = {};
        if(req.body.code_user) objJson.code_user = req.body.code_user; 
        if(req.body.code_session) objJson.code_session = req.body.code_session; 
        if(req.body.code_current) objJson.code_current = req.body.code_current;
        if(req.body.code_relation) objJson.code_relation = req.body.code_relation; 
        if(req.body.code_before) objJson.code_before = req.body.code_before; 
        if(req.body.input) objJson.input = req.body.input; 
        if(req.body.output) objJson.output = req.body.output; 
    
        findData(objJson, function(result){
            res.send(result);
        });
        });

const insertData = function(objJson, callback){
    const collection = db.collection('chatbot');
    collection.insertOne(objJson, function(err, result){
        assert.equal(null,err);
        callback(result);
    });
}

const updateData = function(objJson, callback){
    const collection = db.collection('chatbot');
    const code_current = objJson.code_current;
    collection.updateOne({code_current: code_current}, {$set: objJson} , function(err, result){
        assert.equal(null,err);
        callback(result);
    });
}

const deleteData = function(objJson, callback){
    const collection = db.collection('chatbot');
    collection.deleteOne(objJson,  function(err, result){
        assert.equal(null,err);
        callback(result);
    });
}


const findData = function(objJson, callback){
    const collection = db.collection('chatbot');
    collection.find(objJson).toArray(function(err, result){
        assert.equal(null,err);
        callback(result);
    });
}

MongoConnection(()=>{

    app.listen(3000, () => {
        console.log(`Database running & Listening on port: 3000`);
    })

})

app.get('/question', urlencodedParser, function(req, res){
    let objJson = {};
    if(req.query.code_user) objJson.code_user = Number(req.query.code_user); else objJson.code_user = 0;
    if(req.query.code_session) objJson.code_session = Number(req.query.code_session); else objJson.code_session = 0;
    if(req.query.code_before) objJson.code_before = Number(req.query.code_before); else objJson.code_before = 0;
    if(req.query.input) objJson.input = req.query.input; else objJson.input = '';

    questionData(objJson, function(result){
        res.send(result);
    });
});

const questionData = function(objJson, callback){
    const collection = db.collection('chatbot');
    collection.find(objJson).toArray(function(err, result){
        assert.equal(null,err);
        if(result.length<=0){
            collection.find({code_user:objJson.code_user}).toArray(function(err, result){
                assert.equal(null,err);
                result = nlp(objJson.input, result);
                callback(result);
            });
         } else callback(result);
        
    });
}

const nlp = function(question, array){
    let originalQuestion = question.toString().trim();
    let findInput = 0;
    let findIndex = 0;
    for(let i=0; i<array.length; i++){
        question = question.toString().trim();
        let input = array[i].input.toString().trim()
        if(input.length<=0) input = array[i].output.toString.trim();
        question = question.normalize('NFD').replace(/[\u0300-\u036f]/g,'').toLowerCase();
        input = input.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
        question = question.replace(/[^a-zA-Z0-9\s]/g, '');
        input = input.replace(/[^a-zA-Z0-9\s]/g);

        let tokenizationQuestion = question.split(' ');
        let tokenizationInput = input.split(' ');

        tokenizationQuestion = tokenizationQuestion.map(function(e){
            if(e.length>3) return e.substr(0, e.length-3); else return e;
        });
        tokenizationInput = tokenizationInput.map(function(e){
            if(e.length>3) return e.substr(0, e.length-3); else return e;
        });
        let words = 0;
        for(let x=0; x<tokenizationQuestion.length; x++){
            if(tokenizationInput.indexOf(tokenizationQuestion[x])>=0) words++;
        }
        if(words>findInput){
            findInput = words;
            findIndex = i;
        }
        }
        if(findInput>0) return[{
            "_id": array[findIndex]._id,
            "code_user":array[findIndex].code_user,
            "code_session": array[findIndex].code_session,
            "code_current": array[findIndex].code_current,
            "code_relation": array[findIndex].code_relation,
            "code_before": array[findIndex].code_before,
            "input": originalQuestion,
            "output": array[findIndex].output
        }];
        else return [{
            "_id": "0",
            "code_user":array[findIndex].code_user,
            "code_session": array[findIndex].code_session,
            "code_current": array[findIndex].code_current,
            "code_relation": array[findIndex].code_relation,
            "code_before": array[findIndex].code_before,
            "input": originalQuestion,
            "output": "Desculpe, mas não sei responder."
        }];
}

当我使用邮递员发送我的帖子和获取请求时,增删改查效果很好,集合实际上被更新、删除或插入,但邮递员从不向我发送本地服务器的响应,它总是无限地停留在消息“正在发送请求”上...”因此,我无法使用我的“/find”或我的 get 方法来搜索特定项目

javascript node.js mongodb express postman
1个回答
0
投票

将此用作帖子的示例,我必须删除不必要的断言函数并专注于检查错误

const insertData = function(objJson, callback){
    const collection = db.collection('chatbot');
    collection.insertOne(objJson, function(err, result){
        if (err) {
            console.error(err);
            return callback({ error: 'Unable to insert data' });
        }
        callback(result);
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.