Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Lỗi ở middleware pre('save') của mongoose

    Hỏi Đáp
    0
    4
    344
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Tùng
      Tùng last edited by

      Hi anh/chị

      Em đang dùng mongodb 4.0 và mongoose 4.1.2
      Khi dùng pre('save') trong model để hash password của user trước khi lưu thì không thể lấy được giá trị của this (this = {})
      0_1531472670974_image.png
      Ai đã gặp trường hợp trên và biết cách khác phục xin chỉ em với
      Em cảm ơn.

      1 Reply Last reply Reply Quote 0
      • Acenter Ha
        Acenter Ha last edited by

        Có thể sai nhưng bạn thử sửa 1 tý như bên dưới nhé:

        ArticleSchema.pre('save', function (next) {
        console.log(this);
        next();
        });

        Good luck

        1 Reply Last reply Reply Quote 1
        • Tùng
          Tùng last edited by

          Thanks bác em cũng vừa search ra :))
          Model không được dùng arrow function

          1 Reply Last reply Reply Quote 1
          • Acenter Ha
            Acenter Ha last edited by

            Good job :v

            1 Reply Last reply Reply Quote 0
            • First post
              Last post