2017-06-15 1 views
0

Ich baue eine einfache App, wo ein Unternehmen eine Frage an seine Mitarbeiter sendet, die um Feedback bitten. Ich habe Mühe, mehr Mitarbeiter (Benutzer) in das Fragedokument als ein Array zu setzen. Im Moment wird nur ein Mitarbeiter hinzugefügt. Grundsätzlich benötige ich für jede Frage, dass Mitarbeiter darauf antworten sollen & auch die db für alle Fragen, die ein Mitarbeiter beantwortet hat (in Zukunft) abfragen zu können. Hier sind meine Schemata.Express: POST-Daten zu einem Array mongoose & node

Hier ist die previous issue that was solved - für alle Interessierten.

Modelle

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

Antwort

2

würde ich ändern, um Ihre api.js auf diese Weise:

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

Auch kann ich Ihnen in der Seite der Versprechungen/Generatoren oder Asynchron/erwarten Ansätze suchen vorschlagen. Es wird dann viel einfacher zu lesen.

Gleicher Code mit Asynchron formatiert/erwarten:

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

Arbeitete Dank! Die Async-Methode löst einen "SyntaxError: Unexpected Token Function" -Fehler aus, obwohl sie auf 'async function doJob()' ' –

+0

weist. Wahrscheinlich haben Sie die alte Version von nodejs, aktualisieren Sie sie. – Lazyexpert