【发布时间】:2026-01-03 02:35:02
【问题描述】:
也许这是一个愚蠢的问题,但我想知道如何在 JSON 对象中发布。我是这个后端的新手,所以我还在学习。
这是我的 JSON 方案。
email: {
type: String,
unique: true,
lowercase: true,
required: 'Email address is required',
validate: [validateEmail, 'Please enter a valid email']
},
password: {
type: String
},
role: {
type: String
},
firstName: {
type: String,
},
lastName: {
type: String
},
phone: {
type: Number,
unique: true
},
address: [
{
number: {type: String},
street: {type: String},
city: {tyle: String},
county: {type: String},
postcode: {type: String}
}
],
我在这里发帖。
User.findOne({email: email}, function(err, existingUser) {
if (err) { return next(err) }
if (existingUser) {return res.status(422).json({error: "Email taken"})}
var user = new User({
email: email,
password: password,
role: role,
firstName: firstName,
lastName: lastName,
phone: phone,
number: number,
street: street,
city: city,
county: county,
postcode: postcode
});
因此,除了地址字符串之外,所有内容都在发布。在数据库中我只能看到"address" : []
这是我的注册功能
exports.signup = function(req, res, next) {
var email = req.body.email;
var password = req.body.password;
var role = req.body.role;
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var phone = req.body.phone;
var number = req.body.number;
var street = req.body.street;
var city = req.body.city;
var county = req.body.county;
var postcode = req.body.postcode;
if (!email || !password) {
return res.status(422).json({error: "You must provide an email and password"});
}
【问题讨论】:
-
你没有给
new User添加地址?! -
但是如果我添加的地址必须是一个空变量?因为例如我有
firstName: firstName,但在注册功能中它看起来像这样var lastName = req.body.lastName;
标签: javascript json mongodb