2014-11-08 64 views
0

我有更新待辦事項下面的代碼:用戶節省不,沒有錯誤節省

router.post('/edit/todo', function(req, res) { 
    var post = req.body, 
    newTitle = post.title, 
    newDetails = post.details.replace(/\n/g, '<br>'), 
    // _id = post._id; 
    index = post.index; 

    console.log('req.user.todos[index] = ' + JSON.stringify(req.user.todos[index])); // old details 

    req.user.todos[index].title = newTitle; 
    req.user.todos[index].details = newDetails; 

    req.user.save(function(err) { 
    if (err) return console.log(err); 
    else { 
     console.log('req.user.todos[index] = ' + JSON.stringify(req.user.todos[index])); // new details 
     res.redirect('/'); 
    } 
    }); 
}); 

router.get('/', function(req, res) { 
    var todos = req.user.todos; // old details 
} 

下面是用戶模式:

var UserSchema = new mongoose.Schema({ 
    name: { 
     type: String, 
     required: true 
    }, 

    email: { 
     type: String, 
     unique: true, 
     required: true 
    }, 

    password: { 
     type: String, 
     required: true 
    }, 

    // keeps the tasks like homework and projects 
    // each todo is made of a title, and details 
    todos: [], 

    // keeps the test 
    // each test is made of a subject and a date 
    // each date is made up of a month and a day 
    tests: [], 

    // keeps the schedule 
    // used by tomorrow tile 
    schedule: { }, 

    // links for school related sites 
    // each link is made up of a name and an href 
    links: [] 
}); 

這是很奇怪的,因爲它會保存期通過沒有錯誤,並顯示新的細節,但它似乎實際上並沒有保存用戶。
此外,我創建待辦事項幾乎是一樣的(只需要push而不是放置值),並且完美地工作。

+0

@JohnnyHK更新。 – Gofilord 2014-11-08 17:40:53

回答

2

因爲你沒有定義什麼todo陣列包含在模式中,你需要告訴貓鼬,當你通過調用markModified修改它的元素中的數據:

req.user.todos[index].title = newTitle; 
req.user.todos[index].details = newDetails; 
req.user.markModified('todos'); 
+0

謝謝!我一直試圖弄清楚這兩天了! – Gofilord 2014-11-08 18:13:36