0

コレクションを管理するために 2 つの異なる方法を試しました。

この非常に基本的な状況で、バックボーンおよび mongoDB スキーマでコードを整理するための最良の方法は何か (および 2 つ目の方法が機能しない理由) を知るためにここに来ました。

私の両方の例では、状況collection FriendsModel User

最初の例 (うまくいく)

クライアント側

App.Models.User = Backbone.Model.extend({
    defaults : {
        username : "example",
        Friends: []
    },
});

サーバ側

UserSchema : {
        name: {
            type: String,
            required: true,
            unique: true
        },
        Friends:
        [ {
            user_id: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User'
            },
            status: {
                type: Boolean
            },
        }]

この方法では、Friends referencesからデータを取り込むとUser document、バックボーンはクライアント側から取得したときにコレクションを取得します。

2番目の例(私にはきれいに見えますが、コレクションを取得すると失敗します)

クライアント側

App.Models.User = Backbone.Model.extend({
    defaults : {
        username : "example",
        status: [], //Since Friends is now a collection i build an array to index each user_id according to its status.
        Friends: new App.Collections.User()
    },
});

サーバ側

UserSchema : {
        name: {
            type: String,
            required: true,
            unique: true
        },
        status: [ {
          user_id: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User'
            },
          value: Bolean 
        } ],
        Friends:
        [ {
            user_id: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User'
            }
        }]

アプリ.コレクション.ユーザー

App.Collections.User = Backbone.Collection.extend({
    model: App.Models.User,

    initialize : function() {

    },

    getFriends: function(callback) {
        console.log('getFriends called!');
        this.url = App.url+'friends';
        this.fetch({
            success: function(collection, response, options) {
                console.log('fetch OK');
                console.log(collection.toJSON());
                callback(true);
            },
            error: function(collection, response, options) {
                console.log('fetch KO');
                callback(false);
            }
        });
    }
});

この方法では、 をフェッチするFriends collectionとエラーが発生するUncaught TypeError: undefined is not a function (on chrome)TypeError: this.model is not a constructor (on firebug)、両方ともbackbone-0.9.9.js (ligne 26)

success callbackfromメソッド内にある直前でcollection's fetch、サーバー ide から返されたコレクションがある場合にのみ発生します (バックボーンが解析できない場合など、コレクションが null の場合は問題ありません)。

返される JSON は次のようになります。

   [
      {
          "__v": 0,
          "_id": "5137cdf1538238073f000005",
          "username": "Someone Else",
          "status": [
           {
             "user_id": "5137cb9b730754a666000006",
              value: true
           }
          ],
          "Friends": [
            {
              "user_id": "5137cb9b730754a666000006",
              "_id": "5137cf2e0399fd7c3f000005"
            }
          ],
        },
        "_id": "5137cf2e0399fd7c3f000006"
      }
    ]

では、この両方の方法の間でコードを整理する最良の方法は何ですか? そして、なぜ2番目のエラーでこのエラーが発生すると思いますか(私のJSONは無効ですか?)?

4

1 に答える 1

1

コレクション API からの戻り値が有効な json であることを確認してください。提供された json の戻り値の例は無効です

ここにサンプルのjson文字列をコピーして貼り付けることで、有効性を確認できます

http://jsonlint.com/

于 2013-03-07T10:04:30.503 に答える