postRegistrationHandler: function (account, req, res, next) {
console.log('postRegistrationHandler activated');
account.getCustomData(function(err, data) {
if (err) {
console.log(err.toString, "error string");
return next(err);
} else {
data.mongo_id = userCreationCtrl(account);
data.save();
next();
}
});
},
Diese Funktion fast richtig funktioniert, aber die Zeile:NodeJS ist asynchron und mein Code nicht in der Reihenfolge ausgeführt, erwarte ich
data.save();
läuft, bevor die vorhergehende Zeile endet, was bedeutet, dass die Daten Ich möchte sparen ist nicht zur richtigen Zeit vorhanden.
data.mongo_id = userCreationCtrl(account);
Diese Zeile ruft eine Funktion, die ein mongoDB Dokument mit Informationen auf dem Konto-Objekt erstellt zurückkehrt und dann die _id (das ist, was ich zu retten versuchen.
ich eine .then vielleicht dachte mit () würde helfen, aber das scheint aus irgendeinem Grund nicht verfügbar, hier zu sein Wenn jemand etwas sieht mir persönlich fehlt, die sehr hilfreich sein würde Danke
hier ist die userCreationCtrl Datei wie gewünscht..!
var UserSchema = require('./../models/UserModel.js');
var createNewUser = function (account, res, next){
// We will return mongoId after it is created by submitting a newUser
var mongoId = "";
// Save StormpathID (last 22 characters of account.href property)
var newStormpathId = account.href.slice(account.href.length - 22);
console.log('stormpath ID:', newStormpathId, 'just registered!');
console.log(account);
// Create new user from model by recycling info from the Stormpath registration form and include the stormpathId as well.
var newUser = new UserSchema({
stormpathId: newStormpathId,
firstName: account.givenName,
lastName: account.surname,
email: account.email,
street: account.street,
city: account.city,
zip: account.zip
});
// This saves the user we just created in MongoDB
newUser.save(function(err, result){
console.log(result);
if (err) {
console.error(err);
}
else {
console.log("User created in MongoDB, attempting to return mongoDB _id to stormpath customData");
// Keep track of the new user's mongo _id so we can return it to the previous function and save it as Stormpath custom data.
mongoId = result._id;
console.log(mongoId, "mongoid");
return result._id;
}
});
};
module.exports = createNewUser;
Können Sie nach der Inhalt der 'userCreationCtrl' Funktion? –
Ja, ich habe es in die ursprüngliche Frage bearbeitet. –
'userCreationCtrl' benötigt 3 Argumente und Sie übergeben nur 1. Anstatt von ihr' result._id' zurückzugeben, rufen Sie einfach Callback mit diesem Ergebnis auf und auch innerhalb dieses Callback-Aufrufs 'data.save()' –