2017-06-15 50 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

相关问题