如何在JSON对象中发布?

时间:2018-04-20 15:20:48

标签: javascript json mongodb

也许这是一个愚蠢的问题,但我想知道如何在JSON对象中发布。我是这个后端的新手,所以我还在学习。

这是我的JSON方案。

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" : []

所以一切都是发布地址字符串。在DB中,我只能看到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"}); }

这是我的signUp功能

class Foo(object):
    def __init__(self, x):
        if x > 0:
            self.eval = self.method1
        else:
            self.eval = self.method2


    def method1(self, x):
        return x


    def method2(self, x):
        return x * x


    def compute(self, y):
        return self.eval(y)


f1 = Foo(1)
print(f1.compute(10)) # 10 (method1)

f2 = Foo(-1)
print(f2.compute(10)) # 100 (method2)

1 个答案:

答案 0 :(得分:2)

您应该在地址密钥中发送streetcitycountrypostcode,因为您希望在address下这样做:

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
      }]
});