Node.js 如何根据这些代码添加Json文件?
大家好,我不知道如何根据这些代码添加json文件。Node.js 如何根据这些代码添加Json文件?,node.js,mongodb,mongoose,reference,postman,Node.js,Mongodb,Mongoose,Reference,Postman,大家好,我不知道如何根据这些代码添加json文件。 我想创建一个包含姓名、姓氏、年龄、电话、mevki_id、城市和城镇的用户 注:城市和城镇在土耳其il和ilce中的含义 这是我的代码: 用户架构: var userSchema = Mongoose.Schema({ name:{type: String,require:true}, surname: {type: String,require:true}, tel: {type: String,require:t
我想创建一个包含姓名、姓氏、年龄、电话、mevki_id、城市和城镇的用户 注:城市和城镇在土耳其il和ilce中的含义 这是我的代码: 用户架构:
var userSchema = Mongoose.Schema({
name:{type: String,require:true},
surname: {type: String,require:true},
tel: {type: String,require:true},
age: {type: String,require:true},
mevki_id: {type: String,require:true},
location_id : { type : Mongoose.Schema.Types.ObjectId , ref: 'locations'}
位置架构:
var LocationSchema = Mongoose.Schema ({
il: {type: String, require:true},
ilce: {type:String, require:true}
});
用户控制器:
this.createUser = function (req, res, next) {
user.name = req.body.name;
user.surname = req.body.surname;
user.tel = req.body.tel;
user.age = req.body.age;
user.mevki_id= req.body.mevki_id;
user.location_id=location._id.id;
user.save(function(err, result) {
if (err) {
return res.send({'error':err});
}
else {
return res.send({'result':result,'status':'successfully saved'});
}
});
};
我需要用postman插入什么样的json文件???很明显,您的POST查询数据应该以以下格式发送:
{
name: 'name',
surname: 'surname',
tel: 'tel',
age: 'age',
mevki_id: 'mevki_id',
location_id: 'location_id'
}
我无法确定控制器中的位置。_id.id
变量,因为您没有显示它的来源(代码中的早期?),但您需要根据用户模式位置\u id
类型,将位置id作为数据库中存在位置的引用传递。此id是从LocationSchema定义的位置的ObjectId引用。
您可以省略此元素,因为它在架构中不是必需的
请记住,模式只是数据的结构,而不是数据本身。我不确定您在问什么。你想让JSON文件将用户添加到你的数据库中吗?是的,我想这样做,但我不知道实际上我想知道如何将文档城市和城镇信息添加到用户中,因为userschema中没有城市和数据,它们在location schema中,所以什么样的JSON文件添加到我的数据库中?thnks:)我真的认为你真的是个乐于助人的人如果你想感谢他,记得验证答案;)