【发布时间】:2019-02-01 07:55:23
【问题描述】:
这是我遇到错误的代码。 我尝试了多种方法,但总是遇到同样的错误。 TypeError: _user10.default.aggregate(...).toArray 不是函数 我搜索了其他答案,但它们是 mongodb 而不是 mongoose ex db.collection('users').aggregate 任何帮助将不胜感激
import User from './user.model';
import socket from './../../CHATtest/web/socket.js';
getUserInfo({userId,socketId = false}){
let queryProjection = null;
if(socketId){
queryProjection = {
"socketId" : true
}
} else {
queryProjection = {
"username" : true,
"online" : true,
'_id': false,
'id': '$_id'
}
}
return new Promise( async (resolve, reject) => {
try {
//const result = await User.aggregate([{
//await User.aggregate([{
User.aggregate([{
//DB.collection('users').aggregate([{
$match: {
_id : userId
}
},{
$project : queryProjection
}
]).toArray( (err, result) => {
if( err ){
reject(err);
}
socketId ? resolve(result[0]['socketId']) : resolve(result);
});
} catch (error) {
reject(error);
}
});
},
【问题讨论】:
-
User.aggregate 会返回游标吗? toArray 仅在游标上可用:docs.mongodb.com/manual/reference/method/cursor.toArray
-
我知道 .aggregate 总是返回对象,但这并不意味着您不能将它们放入数组中并在对象中返回数组。也许我不明白这个问题。我是这个的新手
标签: node.js mongodb mongoose socket.io