0

解決!nodejs:用javascript保存可分類菜單的數據併發送給mongodb

我有一個用javascript製作的可排序菜單的彈出窗口。菜單的元素屬於貓鼬模式,我想根據菜單中的新訂單列表更新數據庫。讓我更好地解釋一下。我有這樣的貓鼬模式:

({ 
    title: { type: String }, 
    POI: [{ 
    POIname: { type: String }, 
    POIlocation: { type: String }, 
    POItext: { type: String } 
    }] 
}) 

我通過陣列的POI從快遞到客戶端,

#popupbox 
     #riordinabox 
     #divhead 
      span#exitbox(onclick="closepopupbox('popupbox')", title="EXIT") X 
      p Trascina il POI per riordinare la lista 
     #divlistaPOI 
      each POI in POIs 
      .divpopupbox(id="#{POI.POIname}", draggable="true", ondragenter="dragenter(event)" ondragstart="dragstart(event)") 
       p #{POI.POIname} 
     #divfooter 

,正如你所看到的,.POIname元素創建菜單。用戶可以更改列表的順序並保存新訂單。例如,默認順序爲:

({ 
    title: "sometitle", 
    POI: [{ 
    POIname: "jump", 
    POIlocation: table, 
    POItext: "jump on the table" 
    }], 
    [{ 
    POIname: "run", 
    POIlocation: street, 
    POItext: "run on the street" 
    }], 
    [{ 
    POIname: "turnR", 
    POIlocation: everywhere, 
    POItext: "turn right" 
    }] 
}) 

和排序菜單顯示:

  • 運行
  • turnR

用戶動作後,新的訂單列表是:

  • 運行
  • turnR

現在我想獲得新的訂單,並在數據庫中設置的順序相同,因此,我可以看到

({ 
    title: "sometitle", 
    POI: [{ 
    POIname: "run", 
    POIlocation: street, 
    POItext: "run on the street" 
    }], 
    [{ 
    POIname: "turnR", 
    POIlocation: everywhere, 
    POItext: "turn right" 
    }], 
    [{ 
    POIname: "jump", 
    POIlocation: table, 
    POItext: "jump on the table" 
    }] 
}) 

,我知道哪一步第一。

任何想法?

謝謝!

+0

你爲什麼不使用數組? 'var steps = [{step1},{step2},{step3}]',這樣你就可以像這樣更新步驟'var temp = step [1];步驟[0] =溫度,步驟[1] =步驟[0]' –

+0

是的,但最大的問題是如何發送和設置新訂單到Mongodb! – Angel

+0

你其實可以保存它,整個步驟數組,'document.title =「sometitle」; document.steps = steps; document.save()'這取決於你如何構建你的模式 –

回答

0

@YanMayatskiy我們已經接近尾聲。我這樣做的代碼:

router.get('/:name/orderlist/:list', function(req, res, next) { 
    Itinerario.findOne({name: req.params.name}, function(err, itinerario){ 
    if (err) throw err; 
    var temp = []; 
    var array = JSON.parse(req.params.list); 
    res.send(array); 
    for (i=0; i<array.length; i++){ 
     if (itinerario.POI[i].POIname === array[i]){ 
     temp[i] = itinerario.POI[i]; 
     } else { 
     var j = 0; 
     while (true){ 
      if(itinerario.POI[i].POIname === array[j]){ 
      break; 
      } 
      j++ 
     } 
     temp[j] = itinerario.POI[i]; 
     } 
    } 
    for (i=0; i<temp.length; i++){ 
     itinerario.POI[i] = temp[i]; 
    } 
    itinerario.save(function (err, itinerario){ 
     if (err) throw err; 
     console.log(itinerario); 
    }) 
    }) 
}); 

它工作正常,但有一個問題!最後,當我登錄了「itinerario」的模式,我可以在提示符下看到的步驟的新秩序:

({ 
    title: "sometitle", 
    POI: [{ 
    POIname: "run", 
    POIlocation: street, 
    POItext: "run on the street" 
    }], 
    [{ 
    POIname: "turnR", 
    POIlocation: everywhere, 
    POItext: "turn right" 
    }], 
    [{ 
    POIname: "jump", 
    POIlocation: table, 
    POItext: "jump on the table" 
    }] 
}) 

,但是,如果我做find()方法在蒙戈提示我仍然看到舊秩序的步驟!!!!!!

({ 
    title: "sometitle", 
    POI: [{ 
    POIname: "jump", 
    POIlocation: table, 
    POItext: "jump on the table" 
    }], 
    [{ 
    POIname: "run", 
    POIlocation: street, 
    POItext: "run on the street" 
    }], 
    [{ 
    POIname: "turnR", 
    POIlocation: everywhere, 
    POItext: "turn right" 
    }] 
}) 

Infact當我刷新頁面時,我再次得到舊的訂單。它不會將新訂單保存在數據庫中!

我要瘋了!

+0

解決!只需添加itinerario.markModified('POI');在save()之前。是啊! – Angel

相關問題