Vietnam

    Nodejs.vn

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

    Async khi lưu dữ liệu trong Mongoose

    Hỏi Đáp
    0
    7
    1089
    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.
    • N
      nmanhit last edited by

      Chào anh em, hiện tại mình mới làm quen với Mongoose có vấn đề như sau
      mình có 2 Schema Category và News
      Hiện tại chỉ quan tâm 1 News có thể thuộc về nhiều Category
      Nhưng khi đang thêm 1 list Category chưa xong thì nó đã nhảy xuống thêm News
      => lstCate luôn rỗng
      Cho mình hỏi làm sao thêm xong tất cả Category rồi thì thêm News mới được chạy
      0_1493748306686_113.png

      hidemanvn 1 Reply Last reply Reply Quote 0
      • CodeConCat
        CodeConCat last edited by

        Bạn có thể bỏ đoạn "luu bai viet" vào callback của "them category", mình nghĩ là vậy.

        codeconcat

        1 Reply Last reply Reply Quote 0
        • N
          nmanhit last edited by

          dính vòng for bạn ui 😞

          T 2 Replies Last reply Reply Quote 0
          • T
            trannamit95 @nmanhit last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • T
              trannamit95 @nmanhit last edited by trannamit95

              @nmanhit dung thu Promise.all di ban

              var arrPromise = []
              
              for(.. i < category.length ..){
                var newCate = new Category({
                  Name: category[i]
                })
              
                arrPromise.push(
                  newCate.save()
                )
              }
              
              Promise.all(arrPromise)
              .then(lstCate => {
                var news = new News({
                  Name: ..,
                  Category: lstCate
                })
              
                return news.save()
              })
              .then(data => {
                console.log('success')
              })
              .catch(err => {
                console.log('err: ', err)
              })
              
              1 Reply Last reply Reply Quote 0
              • hidemanvn
                hidemanvn @nmanhit last edited by

                @nmanhit

                var async = require('async');
                var category = [...],
                	lstCate = [];
                var news = ... ; 
                
                async.forEachOf(category, function(value, index, callback) {
                	var newCategory = Category({Name:value});
                	newCategory.save(function(err, doc) {
                		if (!err) {
                			lstCate.push(doc.id);
                      return callback();
                		} else {
                      return callback(err);
                		};
                	});
                }, function(err) {
                  if (!err) {
                    news.save(...);
                  } else {
                    console.error(err);
                  }
                });
                
                1 Reply Last reply Reply Quote 0
                • N
                  nmanhit last edited by

                  Thanks 2 đồng chí rất nhiều, đã mò ra Promise.all , nó hơi ảo diệu chút 🙂

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