javascript node.js从mongoose mongodb获取消息/变量,将钩子传递到express.js中的钩子传递之前

时间:2019-06-11 16:14:47

标签: javascript node.js mongodb express mongoose

我有2个架构,在保存过程中彼此需要数据     操作。 保存模型1后,模型2将需要模型1的ID,并且 完成后,model1将需要model2的ID。

express.js

SCHEMA1.full_name = fullName;
SCHEMA1.other_fields = other_details;

var variableName = 'inside the controller';

schema2 = new schema(req.body);

SCHEMA1.save(function (err, result){
});

SCHEMA1.js

var SCHEMA1 = mongoose.Schema({
    full_name: String,
    other_fields : String//etc
    schema2_foreign_field:{
        type: Schema.Type.ObjectId,
        ref: 'schema2'
    }
})

SCHEMA1.pre('save', function(){
    var fullName = this.full_name;
    var otherFields = this.other_fields;

    var schema2= this.model('schema2')

    schema2.name = 'Bryan';
    schema2.clocking_time = Date.now();

    schema2.save();
    var schema2Id = schema2._id;
    /********************************************
    *   how can i get schema2Id above into pre  *
    *   hook after the data has been saved      *
    ********************************************/
    //perform operation inside schema and compare 
    //result with controller variableName
})

SCHEMA1.post('save', function(){
    SCHEMA1.schema2_foreign_field=schema2Id;//obtained from above
    SCHEMA1.save();
})

1 个答案:

答案 0 :(得分:1)

在架构内声明一个变量,并在两个挂钩中使用它,即 var variableName ='';

Public Function IsEmailAddress( _
    ByVal strEmailAddresses As String) _
    As Boolean

' Checks if strEMailAddr could represent one or more valid e-mail addresses.
' Does not check validity of domain names.
'
' 2003-06-22. Cactus Data ApS, CPH
' 2018-12-01. Expanded to allow for and validate multiple addresses.

  ' Allowed characters.
  Const cstrValidChars    As String = "@_-.0123456789abcdefghijklmnopqrstuvwxyz"
  Const cstrDot           As String = "."
  Const cstrAt            As String = "@"
  ' Minimum length of an e-mail address (a@a.ca).
  Const cintAddressLenMin As Integer = 6
  ' Address separator.
  Const cstrSeparator     As String = ";"

  Dim avarAddresses       As Variant
  Dim Index               As Integer
  Dim strEmailAddr        As String
  Dim strValidChars       As String
  Dim booFailed           As Boolean
  Dim intPos              As Integer
  Dim intI                As Integer

  avarAddresses = Split(strEmailAddresses, cstrSeparator)
  For Index = LBound(avarAddresses) To UBound(avarAddresses)
    strEmailAddr = avarAddresses(Index)
    ' Strip a display name.
    CleanEmailAddress strEmailAddr
    ' Convert to lowercase.
    strEmailAddr = LCase(strEmailAddr)
    ' Check that strEMailAddr contains allowed characters only.
    For intI = 1 To Len(strEmailAddr)
      If InStr(cstrValidChars, Mid(strEmailAddr, intI, 1)) = 0 Then
        booFailed = True
      End If
    Next
    If booFailed = False Then
      ' Check that the first character is not cstrAt.
      booFailed = Left(strEmailAddr, 1) = cstrAt
      If booFailed = False Then
        ' Check that the first character is not a cstrDot.
        booFailed = Left(strEmailAddr, 1) = cstrDot
        If booFailed = False Then
          ' Check that length of strEMailAddr exceeds
          ' minimum length of an e-mail address.
          intPos = Len(strEmailAddr)
          booFailed = (intPos < cintAddressLenMin)
          If booFailed = False Then
            ' Check that none of the last two characters of strEMailAddr is a dot.
            booFailed = (InStr(intPos - 1, strEmailAddr, cstrDot) > 0)
            If booFailed = False Then
              ' Check that strEMailAddr does contain a cstrAt.
              intPos = InStr(strEmailAddr, cstrAt)
              booFailed = (intPos = 0)
              If booFailed = False Then
                ' Check that strEMailAddr does contain one cstrAt only.
                booFailed = (InStr(intPos + 1, strEmailAddr, cstrAt) > 0)
                If booFailed = False Then
                  ' Check that the character leading cstrAt is not cstrDot.
                  booFailed = (Mid(strEmailAddr, intPos - 1, 1) = cstrDot)
                  If booFailed = False Then
                    ' Check that the character following cstrAt is not cstrDot.
                    booFailed = (Mid(strEmailAddr, intPos + 1, 1) = cstrDot)
                    If booFailed = False Then
                      ' Check that strEMailAddr contains at least one cstrDot
                      ' following the sign after cstrAt.
                      booFailed = Not (InStr(intPos, strEmailAddr, cstrDot) > 1)
                    End If
                  End If
                End If
              End If
            End If
          End If
        End If
      End If
    End If
    If booFailed = True Then
      Exit For
    End If
  Next

  IsEmailAddress = Not booFailed

End Function

现在可以在后挂钩函数中使用前挂钩变量