简体   繁体   English

序列化无效值Symbol(ne)

[英]Sequelize invalid value Symbol(ne)

I have the following two files running on an Express Node.js server: 我在Express Node.js服务器上运行以下两个文件:

home.js home.js

var express = require('express')
var sequelize = require('sequelize')
var db = require('../../shared/db.js')

var op = sequelize.Op

var router = express.Router()

router.get('/home', function(req, res, next) {
    db.shared.person.findAll({
        where: {
            email: {
                [op.ne]: null
            }
        },
        order: ['id']
    }).then(function (person) {
        res.locals = {
            person: person
        }
        res.render('home')
    })
})

module.exports = router

db.js db.js

var sequelize = require('sequelize')

var config = {
  host: 'localhost',
  port: 5432,
  username: '...',
  password: '...',
  database: 'postgres',
  dialect: 'postgres',
  operatorsAliases: false
}
var db = new sequelize(config)

module.exports = {
  shared: {
    person: db.define('person', {
      id: {
        type: sequelize.INTEGER,
        primaryKey: true
      },
      name: sequelize.STRING,
      email: sequelize.INTEGER
    }, { freezeTableName: true , timestamps: false, schema: 'shared' }),
  }
}

When I try to run this query, I get an error claiming Unhandled rejection Error: Invalid value { [Symbol(ne)]: null } 当我尝试运行此查询时,出现错误,提示Unhandled rejection Error: Invalid value { [Symbol(ne)]: null }

What am I doing wrong? 我究竟做错了什么? I can use $ne and even ne just fine but they've been deprecated and are not entirely safe to use. 我可以使用$ne甚至ne很好,但是它们已经过时了,使用起来也不是完全安全。 Furthermore, it's not just [op.ne] - I get this error when I use any conditional like this. 此外,不只是[op.ne] -当我使用任何这样的条件时,都会出现此错误。

I'm basing this all on this guide so I'm not really sure what I could be doing wrong here. 我将所有内容都以本指南为基础,因此我不太确定自己在这里可能做错了什么。

Sequelize instance in both db.js and home.js are different, this is because node caches a required module based on it path. 在这两种情况下Sequelize db.jshome.js是不同的,这是因为基于它的路径节点缓存所需的模块。

To solve this issue you can pass around correct instance in db.js 要解决此问题,您可以在db.js传递正确的实例

module.exports = {
  shared: {
    person: db.define('person', {
      id: {
        type: sequelize.INTEGER,
        primaryKey: true
      },
      name: sequelize.STRING,
      email: sequelize.INTEGER
    }, { freezeTableName: true , timestamps: false, schema: 'shared' }),
  },
  db: db
}

Then finally use operators from that shared instance to do query 然后最后使用该共享实例中的运算符进行查询

var express = require('express')
var sequelize = require('sequelize')
var db = require('../../shared/db.js')

var op = db.db.Op;

var router = express.Router()

router.get('/home', function(req, res, next) {
    db.shared.person.findAll({
        where: {
            email: {
                [op.ne]: null
            }
        },
        order: ['id']
    }).then(function (person) {
        res.locals = {
            person: person
        }
        res.render('home')
    })
})

module.exports = router

One more thing, string operators are completely safe to use if you properly sanitize your user inputs. 还有一件事,如果正确清理用户输入,则字符串操作符是完全安全的。 You only need to use secure operators if you are passing un-sanitized user input to Sequelize methods. 仅在将未经处理的用户输入传递给Sequelize方法时,才需要使用安全运算符。

More on this topic 有关此主题的更多信息

Unhandled rejection Error: Invalid value might also appear if you didn't setup string aliases like this: Unhandled rejection Error: Invalid value如果未设置像这样的字符串别名,也可能会出现Unhandled rejection Error: Invalid value

 const Op = Sequelize.Op; const operatorsAliases = { $eq: Op.eq, $ne: Op.ne, ... $any: Op.any, $all: Op.all, $values: Op.values, $col: Op.col }; const connection = new Sequelize(db, user, pass, { operatorsAliases }); 

But, better to remove String based aliases from code and use [Op.ne] for example, Sequlize is planning to deprecate them soon. 但是,最好从代码中删除基于字符串的别名并使用[Op.ne],例如,Sequlize计划很快弃用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM