Postgres后端函数未在节点服务器中返回预期结果

时间:2017-05-16 02:45:02

标签: javascript node.js postgresql

我完成了撞击键盘上的键盘。我使用postgres后端,节点服务器和pghelper在顶部。我有一个功能完美正常。见下文。

服务器端代码

PutAsync

数据库端代码

FirebaseDatabase.net

然后我在这里有另一个函数,当你在db上运行查询时可以正常工作,但是当通过节点服务器调用时它会产生意想不到的结果。

服务器端代码

findAllRegisteredTokensByNotificationId: function(req,res, next){
    try{
        var route = '[GET] - /api/device-tokens/' + req.params.nid;
        Auth.validateHeader(req.headers, route, res, function(){
            if(req && req.params && req.params.nid){
                pgHelper.connectDBAndFindResult('getAllTokensByNotificationId',[req.params.nid], res, function(result){
                    if(result){
                        return res.status(200).json({ data: result.rows, message: 'Retrieved ALL registered device tokens.'});
                    }
                    else{
                        return res.status(500).json({ message: 'Error finding registered device tokens.'});
                    }
                });
            }
            else{
                logger.processError('Error retrieving registered device tokens: Not a valid Request.', res, 500);
            }   
        });
    }   
    catch(err){
        logger.processError('Error finding Device Tokens:' + JSON.stringify(err.message), res,500);
    }
}

数据库端代码

-- FUNCTION: stardb.getallregisteredtokensfornotification(integer)

-- DROP FUNCTION stardb.getallregisteredtokensfornotification(integer);

CREATE OR REPLACE FUNCTION stardb.getallregisteredtokensfornotification(
integer)
RETURNS SETOF "TABLE(eid text, platform text, device_type text)"
LANGUAGE 'plpgsql'
COST 100.0
VOLATILE 
ROWS 1000.0
AS $function$

BEGIN
FOR eid, platform, device_type IN
SELECT DT.eid, DT.platform, DT.device_type
    FROM notification_token NT JOIN device_token DT
    ON NT.token_id = DT.token_id and NT.notification_id = ($1)
LOOP
RETURN NEXT;
END LOOP;
RETURN;     
END;

$function$;

ALTER FUNCTION stardb.getallregisteredtokensfornotification(integer)
OWNER TO stardb;

预期结果

findAllRegisteredTokensByApplicationId: function(req,res, next){
    try{
        var route = '[GET] - /api/device-tokens/tokens/' + req.params.aid;
        Auth.validateHeader(req.headers, route, res, function(){
            if(req && req.params && req.params.aid){
                pgHelper.connectDBAndFindResult('getallregisteredtokensbyappid',[req.params.aid], res, function(result){
                    if(result){
                        return res.status(200).json({ data: result, message: 'Retrieved ALL registered device tokens per App'});
                    }
                    else{
                        return res.status(500).json({ message: 'Error finding registered device tokens.'});
                    }
                });
            }
            else{
                logger.processError('Error retrieving registered device tokens: Not a valid Request.', res, 500);
            }   
        });
    }   
    catch(err){
        logger.processError('Error finding Device Tokens:' + JSON.stringify(err.message), res,500);
    }
},

实际结果

-- FUNCTION: stardb.getallregisteredtokensbyappid(integer)

-- DROP FUNCTION stardb.getallregisteredtokensbyappid(integer);

CREATE OR REPLACE FUNCTION stardb.getallregisteredtokensbyappid(
integer)
RETURNS SETOF device_token 
LANGUAGE 'plpgsql'
COST 100.0
VOLATILE 
ROWS 1000.0
AS $function$

DECLARE
r device_token%rowtype;
BEGIN
FOR r in SELECT token_id, token, eid, platform, device_type, app_id 
         FROM device_token where app_id = ($1)
LOOP
    RETURN next r;
END LOOP;
RETURN;
END;    

$function$;

ALTER FUNCTION stardb.getallregisteredtokensbyappid(integer)
OWNER TO stardb;

我可以提供所需的任何其他代码段。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

RETURNS SETOF "TABLE(eid text, platform text, device_type text)"

更改为

RETURNS TABLE(eid text, platform text, device_type text)

当您使用双引号时,您要么返回setof sometypetable(list types),那么postgres认为"TABLE(eid text, platform text, device_type text)"是一个类型的名称