2014-03-18 44 views
0

我第一次使用Node和Express設置服務器,並且無法保存我在PUT調用中檢索到的響應。這是一項調查 - 我需要使用調查中輸入的「已回覆」對象更新模型。Node.js Express PUT功能 - 保存數據

我在控制檯上看到正確的響應輸出,但接收到「Object [object Object],[object Object],[object Object],[object Object],[object Object]沒有方法'findById'」我的「保存」功能。

預先感謝您。

小貓,questions.json

[ 
{ 
    "id": "favorite-food", 
    "number": "1", 
    "url": "favorite-food", 
    "name": "Favorite Food", 
    "question": "Which of the following best describes your kitty's palatte?", 
    "responded" : "default response", 
    "query": "Which of the following best describes your kitty's palatte?", 
    "answers": { 
     "Grumpy" : "Fresh Water Salmon, no bones, served on china", 
     "Hipster" : "Nothing - trying to fit into newer, tighter jeans", 
     "Pudge" : "Anything and everything my owner is eating", 
     "Bub" : "Mice", 
     "Meow" : "Roaches" 
    } 
}, 
{ 
    "id": "favorite-band", 
    "number": "2", 
    "url": "favorite-band", 
    "name": "Favorite Band", 
    "question": "Your kitty claws at you desperatly when it wants to listen to:", 
    "responded" : "default response", 
    "query": "Which of the following best describes your kitty's palatte?", 
    "answers": { 
     "Bub" : "Country", 
     "Grumpy" : "Mozart. Popular music is for the plebs.", 
     "Pudge" : "z100", 
     "Meow" : "Very heavy metal", 
     "Hipster" : "something long winded" 
    } 
} 

Server.js 

var express = require('express'), 
    http = require('http'), 
    questions = require('./data/kitty-questions'); 

var app = express() 
    .use(express.bodyParser()) 
    .use(express.static('public')); 

    app.get('/questions', function (req, res) { 
    res.json(questions); 
    }); 

    app.post('/questions', function (req, res) { 
     var matches = questions.filter(function (question) { 
     return question.url === req.body.url; 
    }); 

     if (matches.length > 0) { 
      res.json(409, {status: 'question already exists'}); 
     } else { 
      req.body.id = req.body.url; 
      questions.push(req.body); 
      res.json(req.body); 
     } 
    }); 

    app.put('/questions/:question_name', function (req, res) { 
     var matches = questions.filter(function (question) { 
     return question.url === req.params.question_name; 
    }); 

    var catResponse = req.body.responded; 
    console.log(JSON.stringify(catResponse)); 

    return questions.findById(req.params.question_name, function (err, question) { 
     question.catResponse = req.body.responded; 
     return question.save(function (err) { 
      if (!err) { 
      console.log("updated"); 
      } else { 
      console.log(err); 
      } 
     return res.send(question); 
     }); 
    });  

    }); 

app.get('/questions/:question_name', function (req, res) { 
    var matches = questions.filter(function (question) { 
    return question.url === req.params.question_name; 
}); 

    if (matches.length > 0) { 
    res.json(matches[0]); 
    } else { 
    res.json(404, {status: 'invalid survey question'}); 
    } 

}); 

app.delete('/questions/:question_name', function (req, res) { 
    var found = false; 
    items.forEach(function (question, index) { 
     if (question.url === req.params.question_name) { 
     found = index; 
     } 
    }); 

    if (found) { 
     items.splice(found, 1); 
     res.json(200, {status: 'deleted'}); 
    } else { 
     res.json(404, {status: 'invalid survey question deletion'}); 
    } 

    }); 


    app.get('/*', function (req, res) { 
    res.json(404, {status: 'not found'}); 
    }); 

    http.createServer(app).listen(3000, function() { 
    console.log("Server ready at http://localhost:3000"); 
}); 

STRING FROM THE TERMINAL AFTER MAKING PUT CALL: 

Server ready at http://localhost:3000 

類型錯誤:對象[{ 「ID」: 「最喜愛的食品」, 「數字」: 「1」, 「URL」: 「最喜愛的食品」,」名稱「:」最喜歡的食物「,」問題「:」以下哪一項最能描述你的小貓的帕拉特?「,」回答「:」默認回答「,」查詢「:」以下哪項最能描述你的小貓的帕拉特? ,「答案」:{「脾氣暴躁」:「淡水鮭魚,沒有骨頭,在中國服役」,「時髦」:「沒有 - 試圖融入更新,更緊的牛仔褲」,「帕吉」:「任何東西, 「」,「喵」,「蟑螂」}},{「id」:「最愛樂隊」,「數字」:「2」,「url」:「最喜歡樂隊」 ,「名字」:「最喜歡的樂隊」,「問題」:「當你想要聽你的時候, :「,」回答「:」默認回答「,」查詢「:」以下哪一項最能描述你的小貓的帕拉特?「,」答案「:{」Bub「:」Country「,」Grumpy「:」莫扎特。流行音樂是爲平民而設的。「,」Pudge「:」z100「,」Meow「:」非常重的金屬「,」時髦「:」長長的囉嗦「}},{」id「:」最愛藏身之處「, 「數字」:「3」,「網址」:「收藏夾」,「名字」:「收藏夾」,「問題」:「你最有可能在這裏找到你的野獸:」,「回答」:「 「,」答案「:{」Bub「:」在你的肩上「,」脾氣暴躁「:」一個人。任何地方,只有一個人。「,」帕吉「:」在冰箱裏「,」喵「:」放羊其他貓「,」時髦「:」外面,吸菸。「}},{」id「:」 ,「數字」:「4」,「網址」:「最喜歡的朋友」,「名稱」:「最喜歡的朋友」,「問題」:「你的小貓一般相處:」,「回答」:「答案「:{」Bub「:」其他貓「,」脾氣暴躁「:」沒有人「,」帕吉「:」人類,動物,無論何人「,」喵喵「:」服從動物「,」狗「」}},{「id」:「最愛名人」,「數字」:「5」,「網址」:「最愛名人」,「名字」:「最喜歡的名人」,「問題」 「,」回答「:」「,」答案「:{」Bub「:」Meg Ryan「,」Grumpy「:」Jack Nicholson「,」Pudge「:」John Candy「, 「喵」:「是否麥克阿瑟將軍算什麼?」,「嬉皮士」:「佐伊Deschanel的」}}]有沒有方法「更新」

3/19 UPDATE: 

app.put('/questions/:question_name', function (req, res) { 

     var question = questions.filter(function (question) { 
     return question.url === req.params.question_name; 

    }); 

    var defaultResponse = question[0].responded; 
    res.json(defaultResponse); 

    var catResponse = req.body.responded; 

    questions.update({id: req.params.question_name}, function (err, question) { 
    question.catResponse = catResponse; 

    question.save(function (err) { 
     if (!err) { 
      res.send(catResponse); 
     } else { 
      res.send(400); //or something 
     } 

    }); 
    }); 

});  
+0

節點的代碼會更有幫助 – user602525

+0

它的存在 - 服務器下。js部分 – Sizzles27

+0

哦,沒有看到滾動條感謝 – user602525

回答

1

有很多不必要的退貨的事情她e,並且至少它們使代碼易於閱讀。

刪除一些東西,而忽略了比賽的變量,因爲這不是在把自己使用的,這樣的事情可能會更你在找什麼:

app.put('/questions/:question_name', function (req, res) { 

    var catResponse = req.body.responded; 

    questions.update({id: req.params.question_name}, function (err, question) { 
     question.catResponse = catResponse; 

     question.save(function (err) { 
      if (!err) { 
       res.send(question); 
      } else { 
       res.send(400); //or something 
      } 

     }); 
    });  

}); 

* 編輯 *

我認爲questions = require('./ data/kitty-questions');是你的貓鼬模型。您需要提出問題才能成爲工作更新的貓鼬模型。

像:

var mongoose = require('mongoose') 
, Questions = mongoose.model('Question') 

那麼你的問題的模型文件大概是這樣的:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var ObjectId = Schema.ObjectId; 

QuestionSchema = new Schema({ 
    //keys and stuff in here 

}); 

mongoose.model('Question', QuestionSchema); 
+0

謝謝 - 這是更清潔。當我用這個新代碼發出PUT請求時,我得到以下錯誤: – Sizzles27

+0

TypeError:Object [object Object],[object Object],[object Object],[object Object],[object Object] has no method'update' – Sizzles27

+0

你可以發佈你發送的東西嗎?將其字符串化 – user602525