這裏是telecalling收集類型錯誤:collection.group是不是在貓鼬的功能
{
"product": "a",
"demo": true,
"followups": [
{
"followup": "2017-05-03T07:54:41.085Z",
"actiondone": "enquiry"
},
{
"followup": "2017-05-05T07:54:41.085Z",
"actiondone": "followup"
}
],
"createdAt": "2017-05-03T07:54:41.085Z",
},
{
"product": "b",
"demo": false,
"followups": [
{
"followup": "2017-05-04T07:54:41.085Z",
"actiondone": "followup"
},
{
"followup": "2017-05-10T07:54:41.085Z",
"actiondone": "installation"
}
],
"createdAt": "2017-05-04T07:54:41.085Z",
},
{
"product": "a",
"demo": false,
"followups": [
{
"followup": "2017-05-06T07:54:41.085Z",
"actiondone": "followup"
}
],
"createdAt": "2017-05-06T07:54:41.085Z",
}
在這裏,我需要按產品,並得到多少演示,諮詢,followups和裝置完成的數據。
以下是一個
var mongoose = require('mongoose');
var telecalling = mongoose.model('telecalling');
summaryReport: function(request,response){
telecalling.group({
key: {product: 1},
cond: {"createdAt": {"$gte": new Date(request.body.fromdate),"$lte": new Date(request.body.todate)}},
reduce: function(curr, result) {
if(curr.demo==true){
result.demos++;
}
var fups = curr.followups;
fups.forEach(allCounts);
function allCounts(fup){
var action = fup.actiondone.toLowerCase()
if(action=='enquiry'){
result.enquiries++;
}
if(action=='followup'){
result.followups++;
}
if(action=='installation'){
result.installations++;
}
}
},
initial: {enquiries: 0, followups: 0, installations: 0}
}, function(err,res){
if(err){
response.json(err);
}
else{
response.json(res);
}
});
}
我得到類型錯誤控制器:telecalling.group不是一個函數。如果我在shell中執行此操作,我會得到結果爲
[
{
"product" : "Fair Automobiles",
"enquiries" : 7,
"followups" : 15,
"installations" : 0,
"demos" : NaN
},
{
"product" : "Fair Fertilizers",
"enquiries" : 1,
"followups" : 0,
"installations" : 0
}
]
我在哪裏做錯了。請幫助我。
聲明'telecalling'在哪裏?看起來在當前範圍內沒有對這個集合的引用。還要看看您使用的實際API。 ['.group()'](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#group)方法當然存在,但它的調用簽名當然是不同的從shell實現。實際上你應該學習如何使用'.aggregate()',而不是依賴於JavaScript驅動的函數,除非完全不可能處理。這是一個簡單的聚合。 –
@NeilLunn,我在頂部聲明爲var mongoose = require('mongoose'); var telecalling = mongoose.model('telecalling'); –
我的不好。錯過了貓鼬標籤。這使得它更簡單。貓鼬模型沒有'.group()'方法。您可以通過模型上的'.collection'訪問器使用本地驅動程序方法,但是如前所述,您確實應該使用'.aggregate()'。所以這將是一個學習的好時機。 –