appserver 的 /catalog/category/index 接口出现 the $sort key specification must be an object 错误

问题咨询 · tgy3300 · 于 5年前 发布 · 1874 次阅读

上面截图是接口请求参数数据,图中接口地址:/catalog/category/zl-server 是我复制 /catalog/category/index 进行修改的,还没有进行二次开发,测试就出这问题

下面是接口请求返回的错误,之前是没有这问题的,奇怪

{"name":"MongoDB Exception","message":"the $sort key specification must be an object","code":15973,"type":"yii\\mongodb\\Exception","file":"/mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php","line":192,"stack-trace":["#0 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php(739): yii\\mongodb\\Command->execute()","#1 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Collection.php(370): yii\\mongodb\\Command->aggregate('product_flat', Array, Array)","#2 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/product/ProductMongodb.php(660): yii\\mongodb\\Collection->aggregate(Array)","#3 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/product/ProductMongodb.php(546): fecshop\\services\\product\\ProductMongodb->getFrontCategoryProductsGroupBySpu(Array)","#4 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Product.php(430): fecshop\\services\\product\\ProductMongodb->getFrontCategoryProducts(Array)","#5 [internal function]: fecshop\\services\\Product->actionGetFrontCategoryProducts(Array)","#6 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Service.php(54): call_user_func_array(Array, Array)","#7 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/category/Product.php(77): fecshop\\services\\Service->__call('getFrontCategor...', Array)","#8 [internal function]: fecshop\\services\\category\\Product->actionGetFrontList(Array)","#9 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Service.php(54): call_user_func_array(Array, Array)","#10 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/app/appserver/modules/Catalog/controllers/CategoryController.php(523): fecshop\\services\\Service->__call('getFrontList', Array)","#11 /mnt/hgfs/mywww/zl-mark/server/fecshop/appserver/local/local_modules/Catalog/controllers/CategoryController.php(34): fecshop\\app\\appserver\\modules\\Catalog\\controllers\\CategoryController->getCategoryProductColl()","#12 [internal function]: appserver\\local\\local_modules\\Catalog\\controllers\\CategoryController->actionZlServer()","#13 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)","#14 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Controller.php(157): yii\\base\\InlineAction->runWithParams(Array)","#15 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Module.php(528): yii\\base\\Controller->runAction('zl-server', Array)","#16 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction('catalog/categor...', Array)","#17 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))","#18 /mnt/hgfs/mywww/zl-mark/server/fecshop/appserver/web/index.php(114): yii\\base\\Application->run()","#19 {main}"],"previous":{"name":"Exception","message":"the $sort key specification must be an object","code":15973,"type":"MongoDB\\Driver\\Exception\\CommandException","file":"/mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php","line":186,"stack-trace":["#0 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php(186): MongoDB\\Driver\\Manager->executeCommand('fecshop', Object(MongoDB\\Driver\\Command), Object(MongoDB\\Driver\\ReadPreference))","#1 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php(739): yii\\mongodb\\Command->execute()","#2 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Collection.php(370): yii\\mongodb\\Command->aggregate('product_flat', Array, Array)","#3 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/product/ProductMongodb.php(660): yii\\mongodb\\Collection->aggregate(Array)","#4 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/product/ProductMongodb.php(546): fecshop\\services\\product\\ProductMongodb->getFrontCategoryProductsGroupBySpu(Array)","#5 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Product.php(430): fecshop\\services\\product\\ProductMongodb->getFrontCategoryProducts(Array)","#6 [internal function]: fecshop\\services\\Product->actionGetFrontCategoryProducts(Array)","#7 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Service.php(54): call_user_func_array(Array, Array)","#8 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/category/Product.php(77): fecshop\\services\\Service->__call('getFrontCategor...', Array)","#9 [internal function]: fecshop\\services\\category\\Product->actionGetFrontList(Array)","#10 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/Service.php(54): call_user_func_array(Array, Array)","#11 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/app/appserver/modules/Catalog/controllers/CategoryController.php(523): fecshop\\services\\Service->__call('getFrontList', Array)","#12 /mnt/hgfs/mywww/zl-mark/server/fecshop/appserver/local/local_modules/Catalog/controllers/CategoryController.php(34): fecshop\\app\\appserver\\modules\\Catalog\\controllers\\CategoryController->getCategoryProductColl()","#13 [internal function]: appserver\\local\\local_modules\\Catalog\\controllers\\CategoryController->actionZlServer()","#14 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)","#15 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Controller.php(157): yii\\base\\InlineAction->runWithParams(Array)","#16 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Module.php(528): yii\\base\\Controller->runAction('zl-server', Array)","#17 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction('catalog/categor...', Array)","#18 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))","#19 /mnt/hgfs/mywww/zl-mark/server/fecshop/appserver/web/index.php(114): yii\\base\\Application->run()","#20 {main}"]}}
共收到 22 条回复
Fecmall#15年前 0 个赞

1.markdown格式看一下 http://www.fecshop.com/markdown

2.发帖说明:http://www.fecshop.com/topic/624 ,出现问题,仔细看报错log,报错信息是he $sort key specification must be an object,发帖得把报错信息写到title里面

3.打印一下你的信息

#3 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/fancyecommerce/fecshop/services/product/ProductMongodb.php(546): fecshop\\services\\product\\ProductMongodb->getFrontCategoryProductsGroupBySpu(Array)"

$filter打印一下,看看数据

public function getFrontCategoryProducts($filter){
        if (Yii::$service->product->productSpuShowOnlyOneSku) {
            
            return $this->getFrontCategoryProductsGroupBySpu($filter);
        } else {
            
            return $this->getFrontCategoryProductsAll($filter);
        }
    }

4.按照,照葫芦画瓢的方式,找一个可以正常的部分,打印一$filter, 比对一下数据的差异,导致这个问题的原因,应该是你传递的$filter参数格式有问题

5.如果解决问题,请在帖子回复一下出现这个问题的原因,以及如何解决这个问题。

Fecmall#25年前 0 个赞

$filter 打印出来的数据:

array(6) { ["pageNum"]=> int(1) ["numPerPage"]=> int(12) ["orderBy"]=> array(1) { ["score"]=> int(-1) } ["where"]=> array(1) { ["category"]=> string(24) "57b6abfff656f246653bf570" } ["select"]=> array(19) { [0]=> string(3) "sku" [1]=> string(3) "spu" [2]=> string(4) "name" [3]=> string(5) "image" [4]=> string(5) "price" [5]=> string(13) "special_price" [6]=> string(12) "special_from" [7]=> string(10) "special_to" [8]=> string(7) "url_key" [9]=> string(5) "score" [10]=> string(23) "reviw_rate_star_average" [11]=> string(12) "review_count" [12]=> string(5) "score" [13]=> string(12) "review_count" [14]=> string(14) "favorite_count" [15]=> string(10) "created_at" [16]=> string(3) "qty" [17]=> string(11) "final_price" [18]=> string(11) "final_price" } ["group"]=> string(4) "$spu" }
tgy3300#35年前 0 个赞

@Fecshop [[#2楼](#comment2)](#comment2) 我打印出来的 $filter 数据如下

Array
(
    [pageNum] => 1
    [numPerPage] => 
    [orderBy] => 
    [where] => Array
        (
            [category] => 5c2f6c259dc6d61c134bf242
        )

    [select] => Array
        (
            [0] => sku
            [1] => spu
            [2] => name
            [3] => image
            [4] => price
            [5] => special_price
            [6] => special_from
            [7] => special_to
            [8] => url_key
            [9] => score
        )

    [group] => $spu
)

这个 $filter 数据是 /catalog/category/getCategoryProductColl() 函数给的,代码如下,这个函数我没动过


$filter = [
            'pageNum'      => $this->getPageNum(),
            'numPerPage'   => $this->getNumPerPage(),
            'orderBy'      => $this->getOrderBy(),
            'where'        => $this->_where,
            'select'       => $select,
        ];

        $productList = Yii::$service->category->product->getFrontList($filter);

我的请求参数也是按文档的来的

接口文档请求示例

{
    categoryId:"57b6ac42f656f246653bf576",
    sortColumn:"review_count",
    filterAttrs:{"color":"multicolor","size":"M"},
    filterPrice:"20-30"
}

我的请求

{
    categoryId:"5c2f6c259dc6d61c134bf242",
    sortColumn: "",
    filterAttrs: {},
    filterPrice: ""
}
tgy3300#45年前 0 个赞
@fecshop\services\category\Product.php  文件中的 actionGetFrontList 函数

protected function actionGetFrontList($filter)
    {
        $filter['group'] = '$spu';
        $coll = Yii::$service->product->getFrontCategoryProducts($filter);
        $collection = $coll['coll'];
        $count = $coll['count'];
        $arr = $this->convertToCategoryInfo($collection);
        return [
            'coll' => $arr,
            'count'=> $count,
        ];
    }
	
	------------------------------------------------------------
	
	$filter['group'] = '$spu';     这个  $spu  从哪里来的,单引号下这个变量就是个字符串呀………………
	
	--------------------------------------------------
	
	@fecshop\services\product\ProductMongodb.php 文件的 getFrontCategoryProductsGroupBySpu()函数代码如下
	
	public function getFrontCategoryProductsGroupBySpu($filter)
    {
        ...
        $group['_id'] = $filter['group'];
        ...
	}
	
	
	这函数的  $group['_id'] = $filter['group'];
	
	$filter['group']  解析出来是字符串  '$spu'
	
	$group['_id'] = '$spu';     这是什么鬼:disappointed_relieved:
	
	
tgy3300#55年前 0 个赞

代码跟踪调试了几遍,也找不到问题出在哪……………………

Fecmall#65年前 0 个赞

1.再说一遍!!!请把报错信息更新到帖子标题里面:the $sort key specification must be an object

2.fecshop默认的功能没有问题,找一个fecshop使用这个函数的api,然后在fecshop默认的功能上面增删代码,然后就知道那个地方导致的问题了

3.google搜索了下,有几个贴一下链接,你琢磨一下把 https://stackoverflow.com/questions/28071517/the-sort-key-specification-must-be-an-object

https://answer-id.com/28071517

4.你的代码也没有贴上来,本人本地无法重新你的问题,只能你自己解决了。

tgy3300#75年前 0 个赞

一如继往的没有解决问题,卡在这了,使用fecshop appserver 提供的 /catalog/category/index 也出现这问题,这接口我没动过

接口文档请求示例

{

categoryId:"57b6ac42f656f246653bf576",
sortColumn:"review_count",
filterAttrs:{"color":"multicolor","size":"M"},
filterPrice:"20-30"

}

我的请求

{

categoryId:"5c2f6c259dc6d61c134bf242",
sortColumn: "",
filterAttrs: {},
filterPrice: ""

}

Fecmall#85年前 0 个赞

mongodb版本是多少?建议3.6以上 php-mongodb 的版本说一下,是自己搭建的,还是用官方给与的docker搭建的?

可能是环境的问题

fecshop的演示:http://demo.fancyecommerce.com/#/catalog/category/57bea0e3f656f275313bf56e

这个接口是没有问题的,另外你的fecshop是那个版本呢?

tgy3300#95年前 0 个赞

fecshop版本是1.7.0.0

环境不会有问题,我之前不是发过

http://www.fecshop.com/topic/1726

这个帖子吗?

如果环境有问题,这个帖子中的内容就不存在 了

Fecmall#105年前 0 个赞

你自己排查一下吧 @tgy3300 [#9楼](#comment9)

我这里是没有问题的

tgy3300#115年前 0 个赞

我知道问题出在哪里了?不过有点不明白,为什么会出这个错误,特来咨询

出现这问题是因为我新增了接口 /catalog/category/zl-server ,我的做法是

1、新建 @appserver\config\fecshop_local_modules\Catalog.php 文件

2、在该文件里写入代码如下

return [
    'catalog' => [
        'controllerMap' => [
            'category'    => 'appserver\local\local_modules\Catalog\controllers\CategoryController',
        ]
    ]
];

3、新建 @appserver\local\local_modules\Catalog\controllers\CategoryController.php 文件

4、该文件代码如下

namespace appserver\local\local_modules\Catalog\controllers;
use Yii;

class CategoryController extends \fecshop\app\appserver\modules\Catalog\controllers\CategoryController
{
	public function actionZlServer(){
        
        if(Yii::$app->request->getMethod() === 'OPTIONS'){
            return [];
        }
        // 每页显示的产品个数,进行安全验证,如果个数不在预先设置的值内,则会报错。
        // 这样是为了防止恶意攻击,也就是发送很多不同的页面个数的链接,绕开缓存。
        $this->getNumPerPage();
        //echo Yii::$service->page->translate->__('fecshop,{username}', ['username' => 'terry']);
        if(!$this->initCategory()){
            $code = Yii::$service->helper->appserver->category_not_exist;
            $data = [];
            $responseData = Yii::$service->helper->appserver->getResponseData($code, $data);
            
            return $responseData;
        }
        
        // change current layout File.
        //Yii::$service->page->theme->layoutFile = 'home.php';

        $productCollInfo = $this->getCategoryProductColl();
        $products = $productCollInfo['coll'];
        $this->_productCount = $productCollInfo['count'];
        $p = Yii::$app->request->get('p');
        $p = (int)$p;
        $query_item = $this->getQueryItem();
        $page_count = $this->getProductPageCount();
        $this->category_name = Yii::$service->store->getStoreAttrVal($this->_category['name'], 'name');
        $code = Yii::$service->helper->appserver->status_success;
        $data = [
            'name'              => $this->category_name ,
            'name_default_lang' => Yii::$service->fecshoplang->getDefaultLangAttrVal($this->_category['name'], 'name'),
            'title'             => $this->_title,
            'image'             => $this->_category['image'] ? Yii::$service->category->image->getUrl($this->_category['image']) : '',
            'products'          => $products,
            'query_item'        => $query_item,
            'refine_by_info'    => $this->getRefineByInfo(),
            'filter_info'       => $this->getFilterInfo(),
            'filter_price'      => $this->getFilterPrice(),
            'filter_category'   => $this->getFilterCategory(),
            'page_count'        => $page_count,
        ];
        $responseData = Yii::$service->helper->appserver->getResponseData($code, $data);
        
        return $responseData;
    }
}

结果一测试就出现 the $sort key specification must be an object 错误了

tgy3300#125年前 0 个赞

@Fecshop [#10楼](#comment10) ,我的11楼问题,你看看,问题出在哪里,新增的 appserver 接口 和 /catalog/category/index 代码一样,结果测试就出现标题中的错误提示

Fecmall#135年前 0 个赞

你的意思是, /catalog/category/index这个接口没有问题,没有报错,而你开发的/catalog/category/zl-server 这个api有问题?出现上述的错误?

tgy3300#145年前 0 个赞

是的,我单独把我新增的@appserver\config\fecshop_local_modules\Catalog.php文件里的代码注释了,让我新增的接口失效,测试/catalog/category/index接口正常

tgy3300#165年前 0 个赞

@Fecshop [[#15楼](#comment15)](#comment15)

奇怪,哪我的为什么有问题,

你的做法和我一样不,难道是我新建的分类和产品有问题??

Fecmall#175年前 0 个赞

@tgy3300 #16楼 按照你的写法搞的

Fecmall#185年前 0 个赞

您自己排查一下吧,无法重现,帮不上忙了

Fecmall#195年前 0 个赞

教你个办法,报错的是一个函数,你在报错的地方打印数据,比对一下两个接口(一个正确,一个错误)调用的数据有什么不同,然后分析一下。

{"name":"MongoDB Exception","message":"the $sort key specification must be an object","code":15973,"type":"yii\\mongodb\\Exception","file":"/mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php","line":192,"stack-trace":["#0 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Command.php(739): yii\\mongodb\\Command->execute()",

"#1 /mnt/hgfs/mywww/zl-mark/server/fecshop/vendor/yiisoft/yii2-mongodb/src/Collection.php(370): yii\\mongodb\\Command->aggregate('product_flat', Array, Array)"
tgy3300#205年前 0 个赞
$this->getCategoryProductColl()

就是这个函数有问题
tgy3300#215年前 0 个赞

我以新建 @appserver\local\local_modules\Catalog\controllers\CategoryController.php 文件的形式来继承重写@fecshop\app\appserver\modules\Catalog\controllers\CategoryController.php 文件,这种方式新增接口才会出现这种错误,我要是直接在@fecshop\app\appserver\modules\Catalog\controllers\CategoryController.php文件里直接新增接口,就不会出现这种错误,说明还是在继承重写上

Fecmall#225年前 0 个赞

去debug一下报错的底层函数吧,这些只是表象

我的本地无法重现,帮不上忙了,自己解决吧

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics