我第一次使用Node和Express设置服务器,但在保存我在PUT调用中检索的响应时遇到问题。这是一项调查 - 我需要用"响应"来更新模型。在调查中输入的对象。
我确实在控制台中看到了正确的响应输出,但接收到#34;对象[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]没有方法&#39 ; 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
TypeError:Object [{" id":" favorite-food"," number":" 1"," url":"最喜欢的食物","名称":"最喜欢的食物","问题":"以及以下最佳描述了你的小猫的指示牌?",#34;":"默认响应","查询":"其中以下最佳描述了你的小猫的味道?","答案":{" Grumpy":"淡水鲑鱼,没有骨头,服务于中国","时髦":"没什么 - 试图穿上更新,更紧身的牛仔裤",#34; Pudge":"任何东西和我的主人的一切正在吃"," Bub":"小鼠","喵":"蟑螂"}},{" id& #34;:"喜爱波段""数":" 2"" URL":"喜爱波段&# 34;,"名称":"最喜欢的乐队","问题":"当你想听的时候,你的小猫绝望地抓住你:&# 34;,"回复":"默认回复","查询":&# 34;以下哪一项最能描述你的小猫的屁股?","答案":{" Bub":"国家",& #34;脾气暴躁":"莫扎特。流行音乐是为了拍摄。",#34; Pudge":" z100"," Meow":"非常重金属", "时髦":"长篇大论"}},{" id":"最爱 - 藏身","号码&#34 ;:" 3"," url":" favorite-hideout"," name":" Favorite Favoriteout", "问题":"你最有可能在这里找到你的野兽:","回复":""," ;答案":{" Bub":"在你的肩膀"," Grumpy":"独自。在任何地方,只有一个人。"," Pudge":"在冰箱"," Meow":"赶走其他猫", "时髦":"外面,吸烟。"}},{" id":"最喜欢的朋友","数字& #34;:" 4"," url":"收藏的朋友","姓名":"收藏的朋友&#34 ;,"问题":"你的小猫通常相处:","回复":"","答案& #34;:{" Bub":"其他猫"," Grumpy":"没有人。"," Pudge& #34;:"人类,动物,无论是谁。","喵喵":"听话的动物","时髦":&#34 ;狗"}},{" ID":"喜爱名人""数":" 5"&#34 ;网址":"最喜欢的名人","名称":"最喜欢的名人","问题":"您的猫科动物无法得到足够的红地毯助行器:","回复":"","答案":{" Bub&#34 ;:" Meg Ryan"," Grumpy":" Jack Nicholson",&# 34; Pudge":" John Candy"," Meow":" McArthur将军是否算数?","时髦":& #34; Zooey 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 :(得分:1)
这里有很多不必要的回报,至少,它们使代码难以阅读。
删除一些东西并忽略匹配变量,因为在PUT中没有使用它,这样的东西可能更像你正在寻找的东西:
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
}
});
});
});
*的 修改 * 强>
我认为问题=要求(' ./ 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);