2017-08-09 44 views
-1

,同时运行在Node.js的下面的代码,我发现了错误: 类型错误:接下来是不是一个函数接下来是不是一个函数

function lastmessage(collection, path, conf, conf2, next) { 
    let tmp = conf === 0 ? hotelcode : parseInt(hotelcode); 
    db.collection(collection).find({ 
     [path]: tmp 
    }).sort({ 
     'created_at': -1 
    }).toArray(function(error, docs) { 
     if (error) return next(error); 
     if (docs.length > 0) { 
      if (conf2 === 0) { 
       console.log(docs[0].updatedAt); 
       return next(null, docs[0].updatedAt); 
      } 
      console.log(docs[0].created_at); 
      return next(null, docs[0].created_at); 
     } 
     console.log('empty'); 
     return next(null, 'empty'); 
    }); 
} 

我在做什么错?

+1

接下来是什么?你怎么调用'lastmessage',听起来像你没有提供'next'的回调 – ste2425

+0

我认为这看起来很明显:你传递给这个函数的第五个参数不是函数 –

+0

所以,我假设'next'是你的回调函数,所以在调用'lastmessage'的地方,你需要为它提供一个回调函数。 – Rowland

回答

0
// callback function 
function someFunc(holder, document) { 
// do stuff 
} 

// when calling lastMessage, of course there won't be null values. 
lastMessage(null, null, null, null, someFunc); 
1

我想,如果你在这个函数中设置的第五个参数是一个函数,你应该定义名为next first的函数。