2013-02-06 3 views
1

프로젝트에 mapReduce을 사용해야하고 documentation을 따라하기 시작했습니다.Map MongoDB에서 Reduce : undefined가 함수가 아닙니다.

페이지에서 first 예를 따라 test project을 생성했습니다.

나는 몽고에서 test라는 이름의 데이터베이스를 만든, 내가 수집 col_one 예에서 개체를 삽입 :

{ 
    _id: ObjectId("50a8240b927d5d8b5891743c"), 
    cust_id: "abc123", 
    ord_date: new Date("Oct 04, 2012"), 
    status: 'A', 
    price: 250, 
    items: [ { sku: "mmm", qty: 5, price: 2.5 }, 
       { sku: "nnn", qty: 5, price: 2.5 } ] 
} 

내 코드 (예처럼) 간단하다 :

// MongoDB part 
// Create server 

var mapFunction1 = function() { 
    emit(this.cust_id, this.price); 
}; 

var reduceFunction1 = function(keyCustId, valuesPrices) { 
    return Array.sum(valuesPrices); 
}; 

collection.mapReduce(
    mapFunction1, 
    reduceFunction1, 
    { out: "col_two" } 
); 

// Print items from col_two 

이 오류가 발생합니다 :

.../node_modules/mongodb/lib/mongodb/connection/server.js:524 
     throw err; 
      ^TypeError: undefined is not a function 

이 경우, 이 오류가 사라집니다.

collection.mapReduce(
    mapFunction1, 
    reduceFunction1, 
    { out: "col_two" }, 
    function() { 
     // Print items from col_two 
    } 
); 

오류가 사라지는 이유는 무엇입니까?

+1

콜백은'mapReduce'의 필수 매개 변수이기 때문에. [docs] (http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#mapreduce)를 참조하십시오. – JohnnyHK

+0

@ JohnnyHK, 왜 그들은 [여기] (http://docs.mongodb.org/manual/applications/map-reduce/#return-the-total-price-per-customer-id) 콜백을 사용하지 않았습니까? –

+0

이 예제는 동기 인터페이스 인 쉘을 사용하기 때문에. – JohnnyHK

답변

3

셸에서 사용되는 API와 기본 node.js 드라이버의 주요 차이점 중 하나는 node.js 드라이버가 비동기 인 동안 쉘이 동기식이라는 것입니다.

node.js 드라이버가 비동기식이기 때문에 결과를 수신 할 수 있도록 documentation에 표시된대로 mapReduce 호출에 콜백 매개 변수를 제공해야합니다.

관련 문제