2017-06-15 10 views
0

フィードバックを求める従業員に質問を送信するシンプルなアプリケーションを構築しています。私は質問文書の中にもっと多くの従業員(ユーザー)を配列として配置しようとしています。今は1人の従業員しか挿入しません。基本的に私が必要とするのは、従業員が回答したすべての質問に対して(将来)dbにクエリを出すことができるように、各質問に従業員に回答させることです(&)。ここに私のスキーマです。Express:配列へのPOSTデータmongoose&node

ここにはprevious issue that was solvedがあります。興味のある方はこちらをご覧ください。私はあなたを修正します

モデル

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

module.exports = mongoose.model('Question', QuestionSchema); 

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

module.exports = mongoose.model('User', UserSchema); 

api.js

  Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) { 
       //example: is this the right way of creating the array 
       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 to answer >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); 
     } 

enter image description here

enter image description here

答えて

2

このようapi.js:

Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) { 
    const userData = [{ 
    "username": "lindelof", 
    "response": "yes", 
    },{ 
    "username": "bailly", 
    "response": "no", 
    },{ 
    "username": "suzan", 
    "response": "yes", 
    }]; 

    for (const user of userData) { 
    const userModel = new User(user); 
    userModel.questions = [question._id]; 
    // Its async, but in this example - no need to wait untill it is executed 
    userModel.save(); 
    question.employees.push(userModel._id); 
    } 

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

また、私は約束/発電機または非同期の側で見て、あなたをお勧めすることができます/アプローチを待ちます。それははるかに読みやすくなります。非同期でフォーマット

同じコードが/待っています:

async function doJob() { 
    const question = await Question.findOne({ title: 'Should we buy a coffee machine?'}); 

    const userData = [{ 
    "username": "lindelof", 
    "response": "yes", 
    },{ 
    "username": "bailly", 
    "response": "no", 
    },{ 
    "username": "suzan", 
    "response": "yes", 
    }]; 

    for (const user of userData) { 
    const userModel = new User(user); 
    userModel.questions = [question._id]; 
    await userModel.save(); 
    question.employees.push(userModel._id); 
    } 

    await question.save(); 
    console.log(question); 

}; 

// And sure we have to call this function somewhere... 
doJob(); 
+0

働いたおかげで! 'async'メソッドは' async function doJob() 'を指しても" SyntaxError:Unexpected token function "エラーをスローします。 –

+0

あなたは古いnodejsバージョンを持っているでしょう。 – Lazyexpert

関連する問題