2017-06-15 1 views
0

私は、企業がフィードバックを求める従業員に質問を送信するシンプルなWebアプリケーションを構築しています。まだmongodbについて学んでいます。一週間中それを周りに遊んでいる&私はゆっくりとフォーラムの助けを借りてそれをうまくやっていますが、今私はスキーマを設計するための欠陥のある思考プロセスを使用していたことを認識しています。私は当初、ユーザーのレスポンスをフィールドとしてUserSchemaに使用していましたが、これはユーザーのプロパティではなく、変化する(yes/no/null)変数であることを認識したので、 。私は今では別にAnswersSchemaを作成しなければなりません(私は1つが必要だと言われましたが、私はそれを頑固に主張しました - 私がプロジェクトを始めた時点では意味がありませんでした)でる)。私の質問は、routerpostsave操作で、3つのエンティティをすべてリンクするAPIでクエリを変更する方法です。 saveここに表示されている操作コードは機能しますが、ユーザーがそのプロパティの1つとして応答したときのように欠陥があります。だから、私はUserSchemaの応答を削除した後に、フロントエンドの角にユーザーの名前だけが表示されるようになりました。Express:mongodb mongooseリンクエンティティ

var QuestionSchema = Schema({ 
    id   : ObjectId, 
    title  : String, 
    employees : [{ type: ObjectId, ref: 'User'}] 
}); 

var UserSchema = Schema({ 
    username : String, 
    //response : String, 
    questions : [{ type: ObjectId, ref: 'Question'}] 
}); 

//new schema/collection I've had to create 
var AnswerSchema = Schema({ 
    response : {type :String, default:null}, 
    question : { type: ObjectId, ref: 'Question'}, 
    employees : [{ type: ObjectId, ref: 'User'}], 
}) 

module.exports = mongoose.model('Question', QuestionSchema); 
module.exports = mongoose.model('User', UserSchema); 
module.exports = mongoose.model('Answer', AnswersSchema); 

api.js

Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) { 
      //example data 
      var user = new User([{ 
      "username": "lindelof", 
      "response": "yes", 
      },{ 
      "username": "bailly", 
      "response": "no", 
      },{ 
      "username": "suzan", 
      "response": "yes", 
      }]); 

      question.employees = [user1._id]; 
      user.questions = [question._id]; 

      question.save(function(err) { 
       if (err) throw err; 
       console.log(question); 
       user1.save(function(err) { 
        if (err) throw err; 
       }); 
      }); 

     }); 
     console.log('entry saved >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); 
    } 

UPDATE enter image description here

答えて

1

それは、多くの関係に多くのだとあなたは、AnswerSchemaを追加することによって、正しいことをしました。多くのユーザー(従業員)が質問に答えることができます。ユーザーは多くの質問に答えることができます。したがって、2つの間の連想集合として答えを得ることは良いことです。念頭に置いて、この関係では

enter image description here

、あなたのスキーマを少し変更する必要があります。

var QuestionSchema = Schema({ 
    id   : ObjectId, 
    title  : String, 
    //employees : [{ type: ObjectId, ref: 'User'}] 
}); 

var UserSchema = Schema({ 
    username : String, 
    //response : String, 
    //questions : [{ type: ObjectId, ref: 'Question'}] 
}); 

var AnswerSchema = Schema({ 
    response : {type :String, default:null}, 
    question : { type: ObjectId, ref: 'Question'}, 
    employee : { type: ObjectId, ref: 'User'}, //a single employee 
}); 

さて、ある特定のユーザーはちょうど彼とAnswerを検索し、すでに質問に答えたかどうかを知るために質問のID:

Answer.findOne({ 
     question: questionId, 
     employee: userId 
    }) 
    .exec(function(err, answer) { 
     if (err) { 

     } else if (!answer) { 
      //the employee has not answered this question yet 
     } else { 
      //answered 
     } 
    }); 

最後に、submit-answer APIには、questionIdとuserIを含む本文が必要ですd(ログインしている場合は、セッションやトークンからもuserIdを取得できます)。再び応答を感謝@talwan

router.post('/', function(req, res) { 
      //req.body = {question: "594315b47ab6ecc30d5184f7", employee: "594315d82ee110d10d407f93", response: "yes"} 
      Answer.findOneAndUpdate({ 
        question: req.body.question, 
        employee: req.body.user 
       }, 
       req.body, 
       { 
        upsert: true //updates if present, else inserts 
       } 
      }) 
     .exec(function(err, answer) { 
      //... 
     }); 
}); 
+0

を(作成のみcreate機能を使用するため)このルート更新既存の答えは、他にそれを作成します。 'Answer.findOneAndUpdate({ 質問:req.body.question、 従業員:req.body.user'角度のHTTPがAPI側からの応答を返さないので何かが起こっていません。 (req.body); '' Answer.findOneAndUpdate'の直上で、角度から送られた質問、名前、応答のある実際の身体を私に示しています –

+0

後に何が起こるかわからない従業員とレスポンスはエンティティもデータベースに保存されます回答文の中のヌル –

+0

あなたはどこから回答を送りますか?データベースエントリは3つのフィールドすべてを表示しますか? –

関連する問題