Arrays Express:将数据发布到阵列mongoose&;节点

Arrays Express:将数据发布到阵列mongoose&;节点,arrays,node.js,mongodb,express,mongoose,Arrays,Node.js,Mongodb,Express,Mongoose,我正在开发一个简单的应用程序,其中一家公司向其员工发送一个问题,请求反馈。我正在努力将更多员工(用户)作为一个数组放入问题文档中。现在它只插入一名员工。基本上,我需要的是让员工回答每个问题,并且能够(将来)查询数据库中员工回答的所有问题。这是我的模式 这是给感兴趣的人的答案 型号 var QuestionSchema = Schema({ id : ObjectId, title : String, employees : [{ type

我正在开发一个简单的应用程序,其中一家公司向其员工发送一个问题,请求反馈。我正在努力将更多员工(用户)作为一个数组放入问题文档中。现在它只插入一名员工。基本上,我需要的是让员工回答每个问题,并且能够(将来)查询数据库中员工回答的所有问题。这是我的模式

这是给感兴趣的人的答案

型号

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);
         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 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
        }
api.js

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);
         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 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
        }


我会这样修改您的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);
  }
});
此外,我可以建议您查看promises/Generator或async/await方法。这样读起来就容易多了

使用async/await格式化的相同代码:

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();

我会这样修改您的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);
  }
});
此外,我可以建议您查看promises/Generator或async/await方法。这样读起来就容易多了

使用async/await格式化的相同代码:

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();

工作了,谢谢!
async
方法指向
async function doJob()
时抛出“SyntaxError:Unexpected token function”错误。很可能您有旧的nodejs版本,请更新它。谢谢!
async
方法指向
async function doJob()
时抛出“SyntaxError:Unexpected token function”错误。很可能您有旧的nodejs版本,请更新它。