1

Mongoose は、DB 内の最初のオブジェクト _ID 0 が存在するにもかかわらず、null を返しています。同様のコードを使用して最初の投稿を取得できますが、最初のタグを取得できません。最初のタグ、最初の投稿の tag1 を除いて、すべてのデータを取得できます。タグ 4、tag5、tag6 を含む 2 番目の投稿を追加すると、tag4 を含む 2 番目の投稿のすべてのデータを取得できます。後の投稿で tag1 を指すと、取得できません。

いくつかの検索を行ったところ、null が返されるということは、レコードが見つからないことを意味することがわかっています。また、findById に加えて find と findOne を使用してみましたが、同じ結果が得られました。レコードが存在する場合、どこが間違っているのかわかりません。おそらくもっと良い方法があると確信しています。

助けてくれてありがとう。

node-webkit で mongoose、mongoose-simpledb、および tungus を使用しています。

タグを保存:

$.each(tagArray, function(i, t) {
    var tags = db.Tags();
    tags.nextCount(function(err, count) {
        post.tags.push(count + i); //tag ID to post
    });
    tags.text= tagArray[i]; //tag text
    post.nextCount(function(err, count) {
        tags.posts.push(count); //post ID to tag
    });
    tags.save(function(err) {
        if (err) {throw err;}
        console.log('tag saved');
    });
});

タグを検索:

$.each(results[i].tags, function(j, t) {
    db.Tags.findById(results[i].tags[j], function(err, tag) {
        if (err) {return console.error(err);}
        if (!tag) {return console.log('Could not find tag...');}
        postContent += '<a href="">' + tag.text + '</a> ';
    });
});

または、次のタグを使用すると、返されるタグ1の代わりにタグ2が返されます。

db.Posts.
    find({}).
    populate('tags').
    exec(function(error, post) {
        console.log(post[0].tags[0].text);
    });

タグ モデル

var ObjectId = require('mongoose-simpledb').Types.ObjectId;

exports.schema = {
    _id: Number,
    text: String,
    posts: [{type: ObjectId, ref: 'Posts', index: true}]
};

タグデータベース

{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":0,"_uid":1,"_dt":1409869458919,"_s":"c245621efdb176d3f4dd2db749590730"}
{"_id":0,"text":"Tag1","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":1,"_uid":1,"_dt":1409869458921,"_s":"80bc4453ee777de0177b27ba76ddc859"}
{"_id":1,"text":"Tag2","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":2,"_uid":1,"_dt":1409869458930,"_s":"12682b1c27ea57e8c09b87a2a6605510"}
{"_id":2,"text":"Tag3","posts":[{"$wrap":"$oid","v":0}],"__v":0}

タグ DB での検索の使用:

db.Tags.findOne({
    _id: '0'
}, function(err, result) {
    if (err) return console.error(err);
    console.dir('findOne: ' +result.text);
    //throws error - Cannot read property 'text' of null
});

db.Tags.
findById(0, function(err, tag) {
    if (err) return console.error(err);
    console.log('tag by id: ' + tag);
    //returns - null
});

db.Tags.
find({}).
exec(function(error, tag) {
    console.log("find:" + tag[0]);
    //returns - ( _id: 0, text: 'Tag1', _v: 0, posts: [0] )
    //correctly finding the tag but not by id
});

これらの検索関数を最初のタグ以外のタグを検索するように変更すると、正しい情報が返されます。

4

1 に答える 1

0

問題は mongoose-simpledb であることが判明しました。問題が mongoose-simpledb のバグなのか tungus との競合なのかはわかりませんが、mongoose-simpledb が削除されると、「同じ」コードが正常に機能します。

于 2014-09-11T01:34:48.700 に答える