架构错误意味着app [英] schema error mean app
问题描述
我有架构问题。我在api中没有得到正确的架构。这是我的api:
I have a schema problem. I dont get the right schema in my api. here is my api :
var Meetup = require('./models/meetup');
module.exports.create = function (req, res) {
var meetup = new Meetup(req.body);
meetup.save(function (err, result) {
console.log(result);
res.json(result);
});
}
module.exports.list = function (req, res) {
Meetup.find({}, function (err, results) {
res.json(results);
});
}
console.log显示{__v:0,_id:58343483ff23ad0c40895a00}应显示{__v:0,名称:'文字输入',_ id:58343076b80874142848f26e}
The console.log displays { __v: 0, _id: 58343483ff23ad0c40895a00 } while it should display { __v: 0, name: 'Text input', _id: 58343076b80874142848f26e }
这是我的模特:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Meetup = new Schema({
name: String,
});
module.exports = mongoose.model('Meetup', Meetup);
推荐答案
如果 req.body
未定义(正如您在评论中所写)然后显然新的Meetup(req.body);
无法用任何数据填充新对象(如{ name:'text input'}或其他任何内容)因为它是以 undefined
作为参数调用的。
If req.body
is undefined (as you wrote in the comments) then obviously new Meetup(req.body);
cannot populate the new objects with any data (like {name: 'Text input'} or anything else) since it is called with undefined
as an argument.
Make确保您使用正文解析器并在请求中传递正确的数据。
Make sure you use the body-parser and that you pass the correct data in your request.
另外,检查错误。采用错误
参数的每个回调应采用以下形式:
Also, check for errors. Every callback that takes the err
argument should be in the form of:
module.exports.list = function (req, res) {
Meetup.find({}, function (err, results) {
if (err) {
// handle error
} else {
// handle success
}
});
}
如何追踪问题:
- 请确保使用正文解析器在后端
- 确保在前端传递正确的数据
- 确保前端传递的数据位于正确的位置(正确)
- 确保数据格式正确(JSON?URL编码?)
- 添加
控制台.log(req.body)
在之后新的Meetup(req.body);
知道你节省了什么 - 在浏览器的开发者控制台中打开网络选项卡,查看转移的内容
- make sure you use the body-parser on the backend
- make sure you pass the correct data on the frontend
- make sure that the data passed by your frontend is in the correct place (body)
- make sure that the data is in the correct format (JSON? URL-encoded?)
- add
console.log(req.body)
afternew Meetup(req.body);
to know what you save - open the Network tab in the developer console of your browser and see what is transferred
这篇关于架构错误意味着app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!