0

node.js + mongoose + websocketを使用して趣味の「civlike」ゲームを作成しています(これはバックエンドのみで、主にトレーニング用です)。「テクノロジー」に固執しました。実装方法がわかりません。

最初のアイデアはexec()メソッドを「オーバーロード」することで、次のようなものを追加します。

Techs.apply(result);    

、しかしそれはそのように行うことはできません-それは非同期であり、exec()は返されたドキュメントへのアクセスを持っていません。

それがなければ、ドキュメントをクエリするたびに手動で適用する必要があります-しかし、それを行うための最善の方法はないと思います、おそらく間違っているか、それがそれを行う唯一の方法ですか?

4

1 に答える 1

0

非同期であるため、コールバック関数でドキュメントを処理する必要があります。あなたがやろうとしているのはこれだと思います。

IDでドキュメントを取得します

    // docId is the _id passed from the client
    Techs.findById(docId, function(err, doc) {

        // This code executes after we've returned from MongoDB with the answer
        // Now you can either updated it some code like this
        doc.title = 'new title';

        // Then after you've updated you need to call the save() function
        doc.save();

        // Now send the response to the client
        response.end(doc); // this will send the JSON of the doc to the client

    });

また

フィールドで1つのドキュメントを検索します(タイプ:「iphone」)

    // Find a document based on a query
    Techs.findOne({type: 'iphone' }, function(err, doc) {

        // This code executes after we've returned from MongoDB with the answer
        // Now you can either updated it some code like this
        doc.title = 'new title';

        // Then after you've updated you need to call the save() function
        doc.save();

        // Now send the response to the client
        response.end(doc); // this will send the JSON of the doc to the client

    });
于 2012-09-06T23:28:09.667 に答える