代码之家  ›  专栏  ›  技术社区  ›  Alberto José

knex:不允许操作员“未定义”

  •  0
  • Alberto José  · 技术社区  · 7 年前

    我试图在nodejs中使用knex,其中接收到来自参数的消息,但显示消息“TypeError:the operator“undefined”is not allowed”,这是我代码的一部分,控制台已关闭

    async retrieve(where, fields) {
        console.log(where);
        var _this = this;
        return new Promise((resolve, reject) => {
            //si no tengo where lo pongo como objeto vacio
            where = where || {};
            //si no tengo fields cojo los de defecto del schema
            if(!fields){
                fields = Object.keys(_this.schema.properties).map((key) => {
                    return key;
                });
            }
            //selecciono
            console.log('este es el where justo antes de knex', where);
            _this.connection(_this.table)
            .where(where)
            .select(fields)
            .then((rows) => {
                resolve(rows);
            })
            .catch((error) => {
                console.log(error);
                reject(GlobalMessages.db.dbError);
            });
        });
    }
    

    knex function

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   tuomassalo    6 年前

    有没有可能 where 实际上是一根绳子 "{id_comercio:50}" 而不是一个物体?这可以解释你所犯的错误。

        2
  •  0
  •   Mikael Lepistö    7 年前

    下面是代码的简化版本:

    async retrieve(whereClause, fields) {
      whereClause = whereClause || {};
      fields = fields || Object.keys(this.schema.properties);
    
      try {
        return await this.connection(this.table).where(whereClause).select(fields);
      } expect (error) {
        console.log(error);
        throw GlobalMessages.db.dbError;
      }
    }
    

    效果更好吗?不过,我找不到任何明显会导致这种错误的东西。它应该发生在查询构建期间,但正如您从这个测试代码中看到的那样 https://runkit.com/embed/mcb4fm8gmrhw 上面的代码运行得很好。

    也许那不是抛出错误的地方?您可以添加更多调试打印,以查看在执行该select行后代码中可能发生的其他情况。

        3
  •  0
  •   Maxim Firsoff    6 年前

    请检查一下: where = where || {}; 如果传递的是原始字符串而不是对象,则会出现相同的错误。

    推荐文章