2012-11-12 2 views
0

을 mongoskin로 문자열을 제대로 반환 할 수 없습니다그래서 나는이 스크립트가

{"title": "Hello World", "postNumber": 0, "_id": ObjectId("509eeffbf8f11e8813000001")} 

내 목표는 문자열에 "Hello World를 얻을 받고 있습니다 "콘솔에 출력, 대신 내가 할이 :

{ emitter: { _events: { open: [Object] }, _maxListeners: 50 }, 
    state: 1, 
    options: undefined, 
    skinDb: 
    { emitter: { _events: [Object], _maxListeners: 100 }, 
    state: 1, 
    _dbconn: 
     { databaseName: 'titles', 
    serverConfig: [Object], 
    options: [Object], 
    _applicationClosed: false, 
    native_parser: true, 
    bsonLib: [Object], 
    bson: {}, 
    bson_deserializer: [Object], 
    bson_serializer: [Object], 
    _state: 'connecting', 
    pkFactory: [Object], 
    forceServerObjectId: false, 
    safe: true, 
    notReplied: {}, 
    isInitializing: true, 
    auths: [], 
    openCalled: true, 
    commands: [], 
    _callBackStore: [Object], 
    logger: [Object], 
    slaveOk: false, 
    tag: 1352682584657, 
    eventHandlers: [Object], 
    serializeFunctions: false, 
    raw: false, 
    recordQueryStats: false, 
    reaperEnabled: false, 
    _lastReaperTimestamp: 1352682584657, 
    retryMiliSeconds: 1000, 
    numberOfRetries: 60, 
    reaperInterval: 10000, 
    reaperTimeout: 30000, 
    readPreference: undefined }, 
db: null, 
username: '', 
password: undefined, 
admin: { emitter: {}, state: 0, skinDb: [Circular], admin: null }, 
_collections: { titles: [Circular] }, 
bson_serializer: 
    { BSON: [Object], 
    Long: [Object], 
    ObjectID: [Object], 
    DBRef: [Function: DBRef], 
    Code: [Function: Code], 
    Timestamp: [Object], 
    Binary: [Object], 
    Double: [Function: Double], 
    MaxKey: [Function: MaxKey], 
    MinKey: [Function: MinKey], 
    Symbol: [Function: Symbol] }, 
ObjectID: 
    { [Function: ObjectID] 
    index: 0, 
    createPk: [Function: createPk], 
    createFromTime: [Function: createFromTime], 
    createFromHexString: [Function: createFromHexString] } }, 
    ObjectID: 
    { [Function: ObjectID] 
index: 0, 
createPk: [Function: createPk], 
createFromTime: [Function: createFromTime], 
createFromHexString: [Function: createFromHexString] }, 
    collectionName: 'titles', 
    collection: null, 
    internalHint: null, 
    hint: [Getter/Setter] } 

내가 함수 내()는 CONSOLE.LOG를 넣을 때 잘 작동, 어떤 이유로 문자열을 반환 할 수 없습니다.

해결 : 당신은 자바 스크립트 콜백을 이해할 필요가

read = function(callback){ 
    titles.findOne(function(err, result){ 
     if (err) throw err; 
     callback(result.title); 
    }); 
}; 

read(function(title){ 
console.log(title); 
}); 

답변

0

.

1 - findOne은 문자열 결과가 아닌 db 객체를 반환합니다. mongoskin doco는 읽지 않았지만 db 컬렉션이나 콘솔에서 볼 수있는 일종의 정렬이라고 생각합니다. 로그를 출력하십시오. 2 - 콜백에서 return result.title; 기대했던대로 타이틀을 돌려주지 않을 것입니다. 그래서 콜백 작업 방법에 대한 독서를 제안합니다.

제목을 인쇄하는 올바른 방법은 언급 한대로 console.log를 콜백 함수에 넣는 것입니다.

+0

감사합니다. 자습서를 찾아 보겠습니다. – user1816679

+0

나는 그것을 발견하고 해결했다. 제안 해 주셔서 감사합니다! – user1816679

관련 문제