0

我正在学习 Node.js。我正在创建一个像 Eventbrite 这样的网站。为了进行有效的模糊搜索,我正在实现fuse.js以通过EventNameVenue一段时间来搜索事件date 。我不知道为什么我会收到这个错误。我该如何解决?

(节点:3672)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。

我在下面附上我的代码:

//Have a look in app.get()

const express = require('express');
const hbs = require('hbs');
const fs = require('fs');
const Fuse = require('fuse.js');
const MongoClient = require('mongodb');
const port = process.env.PORT || 5050;




var app = express();
var bodyParser = require('body-parser');


app.set('view engine', 'hbs');

hbs.registerPartials(__dirname + '/views/partials/');
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(bodyParser.json());

//to import json in db
//mongoimport --db Gather --collection Events --file JsonDataV2.txt --jsonArray

app.use(express.static(__dirname + '/public'));

let query;
let location;

//???? Getting problem here '/'
app.get('/',(req, res) => {
    MongoClient.connect('mongodb://localhost:27017/Gathered',{ useNewUrlParser: true }, (err,client) => {
        if(err){
            return console.log('Unable to connect to MongoDb server.');;
        }
        try {
          const db = client.db();
        db.collection('Events').find().toArray().then((docs)=>{
          console.log('query proccessed');

          // Fuse.js implementation goes here...
        var options = {
          keys: ['EventName','Organiser','Venue','Time','Details']
        };
        
        var fuse = new Fuse(docs, options);
        docs =  fuse.search(query);


        
        res.render('home.hbs',{
          data: docs,   
          query:query,
          location:location,
          noOfResult:docs.length,
          });

        });
        } catch (error) {
          console.log(error);
          
        }
        
        
        

        client.close();
    });

  
  
  
//   res.render('home.hbs',{
//     pageTitle: 'Home',
//     welcomeMessage: 'Welcome to Home Page :)'
//   });
});

app.post('/',function(req,res){
   query = req.body.query;     //recieve variable by name from frontend
   location = req.body.location;
  console.log("query: ",query);
  console.log("location: ",location);
  // res.send("Working");


});


app.listen(port, () => {
	console.log(`Server is up at port ${port}`);
});

4

2 回答 2

0

你应该做这样的事情

 db.collection('Events').find().toArray().then((docs)=>{
      console.log('query proccessed');
       //--do what ever--//
       }).catch((err)=>{
         console.log(err);
       //or handle it as you seem it fit
         })
于 2019-03-01T06:31:15.493 回答
0

不推荐使用promise has resolveandreject进行拒绝而没有捕获来处理它。

添加一个 catch 来处理 Promise 拒绝。

db.collection('Events').find().toArray().then((docs)=>{
  console.log('query proccessed');
}).catch(function () {
  console.log("Promise Rejected");
});
于 2019-03-01T06:30:29.367 回答