gpt4 book ai didi

javascript - 如何在 JSON 对象中发布?

转载 作者:行者123 更新时间:2023-12-01 02:07:58 26 4
gpt4 key购买 nike

也许是一个愚蠢的问题,但我想知道如何在 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"});
}

最佳答案

您应该在地址键中发送街道城市国家邮政编码,正如您所期望的那样在地址下,如下所示:

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,
address: [{
street: street,
city: city,
county: county,
postcode: postcode
}]
});

关于javascript - 如何在 JSON 对象中发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49944935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com