2017-06-15 54 views
0

我正在構建一個簡單的應用程序,公司向其員工發送問題以請求反饋。我努力將更多員工(用戶)作爲一個數組放在問題文檔中。現在它只能插入一名員工。基本上我需要的是每個問題都有員工迴應&也能夠(在未來)查詢數據庫中員工已回答的所有問題。這是我的模式。Express:將數據發佈到數組mongoose和節點

這裏的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'方法通過指向'異步函數doJob()'' –

+0

'拋出一個「SyntaxError:Unexpected token function」錯誤,很可能你有舊的nodejs版本,請更新它。 – Lazyexpert

相關問題