在sequelize + postgres中更新我的JSONB数据类型中的详细信息

时间:2018-04-05 08:27:35

标签: javascript postgresql sequelize.js

如何在sequelize + postgres中更新JSONB数据类型的详细信息?这是我迄今为止所能做到的......我想要一个用户可以注册商家并输入他们的位置并在locatri中的场景

`// update Business controller
import db from '../models';

const business = db.Business;

class UpdateBusiness{
    static updateBusiness(req, res){
        business.sync({force: false}).then(
            () => {
              const location = JSON.stringify({
                address: req.body.address,
                city: req.body.city,
                country: req.body.country,
                state: req.body.state
              });
              business.findAll({
                  where: {
                    id: req.params.businessId,
                    userId: req.decoded.userId,
                    location: {
                        $contains: location
                    } 
                  }
              }).then(businesses => {
                    if(!businesses){
                        return res.status(404).send({
                            message: 'Business does not exist...'
                        })
                    }
                    business
                    .updateAttributes(req.body, { fields: Object.keys(req.body)}, {
                        where: {
                            location: {
                                $contains: location
                            }   
                        }
                    }
              ).then(modifyBusiness => res.status(200).send({
                          modifyBusiness
                      })).catch(error => {
                          return console.log(error.message)
                          res.status(500).send( error.message )
                      })
                }).catch(error => {
                    return console.log(error.message)
                    res.status(500).send( error.message )
                })
            });
    }
}
export default UpdateBusiness;

这是我用npm start运行上面的代码时得到的错误。当我点击api时,我正在寻找一种更新我的jsonB数据的方法。

// error that resulted

The server is listening on port 4000
Executing (default): CREATE TABLE IF NOT EXISTS "Businesses" ("id" UUID NOT NULL , "name" VARCHAR(255) NOT NULL, "mobile" VARCHAR(255) NOT NULL, "email" VARCHAR(255) NOT NULL UNIQUE, "locatio
n" JSONB NOT NULL DEFAULT '{}', "category" TEXT NOT NULL, "services" TEXT NOT NULL, "workingHours" VARCHAR(255) NOT NULL, "workingDays" VARCHAR(255) NOT NULL, "createdAt" TIMESTAMP WITH TIME
ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, "userId" UUID REFERENCES "Users" ("id") ON DELETE SET NULL ON UPDATE CASCADE, UNIQUE ("email"), PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column
_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.
relkind = 'r' and t.relname = 'Businesses' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT "id", "name", "mobile", "email", "location", "category", "services", "workingHours", "workingDays", "createdAt", "updatedAt", "userId" FROM "Businesses" AS "Busine
ss" WHERE "Business"."id" = 'b020732c-566b-43dd-a7f3-01d982b3810a' AND "Business"."userId" = '8e5317f8-21b9-469a-bfe8-9f0fa764edb3' AND "Business"."location" @> '"{\"address\":\"No 8 saka son
ioki street\",\"city\":\"Ikeja\",\"country\":\"Ghana\",\"state\":\"Lagos\"}"';
businesses.updateAttributes is not a function

0 个答案:

没有答案