I have a business leve database module called "db_location" which uses the node-fetch
module to get some data from a remote server via REST API.
我有一个名为“db_location”的业务级别数据库模块,它使用node-fetch模块通过REST API从远程服务器获取一些数据。
**db_location.js** DB LOGIC
const p_conf = require('../parse_config');
const db_location = {
getLocations: function() {
fetch(`${p_conf.SERVER_URL}/parse` + '/classes/GCUR_LOCATION', { method: 'GET', headers: {
'X-Parse-Application-Id': 'APPLICATION_ID',
'X-Parse-REST-API-Key': 'restAPIKey'
}})
.then( res1 => {
//console.log("res1.json(): " + res1.json());
return res1;
})
.catch((error) => {
console.log(error);
return Promise.reject(new Error(error));
})
}
};
module.exports = db_location
I would need to call this function within a Route function so as to separate database processing from controller.
我需要在Route函数中调用此函数,以便将数据库处理与控制器分开。
**locations.js** ROUTE
var path = require('path');
var express = require('express');
var fetch = require('node-fetch');
var router = express.Router();
const db_location = require('../db/db_location');
/* GET route root page. */
router.get('/', function(req, res, next) {
db_location.getLocations()
.then(res1 => res1.json())
.then(json => res.send(json["results"]))
.catch((err) => {
console.log(err);
return next(err);
})
});
When I ran http://localhost:3000/locations, I received the following error.
当我运行http:// localhost:3000 / locations时,我收到以下错误。
Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
It seems the Promise was empty or something wrong down the Promise chain going from one response
object to another? What is a best practise for solving this kind of scenario?
似乎Promise是空的,或者Promise链从一个响应对象转到另一个响应对象时出现了什么问题?解决这种情况的最佳做法是什么?
EDIT 1
编辑1
If I changed the getLocations to return res1.json() (which I think is a non-empty Promise according to the node-fetch
documentation):
如果我更改了getLocations以返回res1.json()(根据node-fetch文档,我认为它是非空的Promise):
fetch(`${p_conf.SERVER_URL}/parse` + '/classes/GCUR_LOCATION', { method: 'GET', headers: {
'X-Parse-Application-Id': 'APPLICATION_ID',
'X-Parse-REST-API-Key': 'restAPIKey'
}})
.then( res1 => {
return res1.json(); // Not empty as it can be logged to `Promise Object`
})
.catch((error) => {
console.log(error);
return Promise.reject(new Error(error));
})
And the route code was changed to :
并且路线代码更改为:
db_location.getLocations()
.then(json => res.send(json["results"]))
.catch((err) => {
console.log(err);
return next(err);
})
The exactly same error was thrown.
抛出了完全相同的错误。
2 个解决方案
#1
2
You need getLocations
to return a Promise
. At the moment, it's running a fetch
, but that fetch
isn't connected to anything else, and getLocations
is returning undefined
(and of course you can't call .then
on uundefined
)
你需要getLocations来返回一个Promise。目前,它正在运行一个fetch,但是fetch没有连接到任何其他东西,并且getLocations返回undefined(当然你不能在uundefined上调用.then)
Instead, change to:
相反,改为:
const db_location = {
getLocations: function() {
return fetch( ...
Also, since you're not doing anything special in the getLocations
catch
block, you might consider omitting it entirely and let the caller handle it.
此外,由于您没有在getLocations catch块中执行任何特殊操作,因此您可以考虑完全省略它并让调用者处理它。
#2
0
Your function doesn't return anything.
你的功能没有返回任何东西。
If you want to use a promise, you need return
it.
如果您想使用承诺,则需要退货。
#1
2
You need getLocations
to return a Promise
. At the moment, it's running a fetch
, but that fetch
isn't connected to anything else, and getLocations
is returning undefined
(and of course you can't call .then
on uundefined
)
你需要getLocations来返回一个Promise。目前,它正在运行一个fetch,但是fetch没有连接到任何其他东西,并且getLocations返回undefined(当然你不能在uundefined上调用.then)
Instead, change to:
相反,改为:
const db_location = {
getLocations: function() {
return fetch( ...
Also, since you're not doing anything special in the getLocations
catch
block, you might consider omitting it entirely and let the caller handle it.
此外,由于您没有在getLocations catch块中执行任何特殊操作,因此您可以考虑完全省略它并让调用者处理它。
#2
0
Your function doesn't return anything.
你的功能没有返回任何东西。
If you want to use a promise, you need return
it.
如果您想使用承诺,则需要退货。