如何解决未处理的Promise节点
我是新手,目前正在使用React处理约会应用程序的教程。我也在使用cosmicjs CMS。它在localhost上启动,但只说在底部进行加载,并且不允许我与页面进行交互。当我运行npm start时,出现以下错误
GET / 304 3.235 ms - -
GET /bundle.js 304 1.092 ms - -
GET /863be562288d82b810f1a477f15c0254.svg 304 0.399 ms - -
GET /bundle.map.js 304 0.426 ms - -
GET /favicon.ico 302 3.647 ms - 23
GET / 200 1.857 ms - 391
(node:3030) UnhandledPromiseRejectionWarning:
TypeError: Cannot read property 'metadata' of undefined
at /Users/ayr/Desktop/appointment-scheduler/app.js:84:34
at /Users/ayr/Desktop/appointment-scheduler/node_modules/cosmicjs/index.js:92:16
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3030) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3030) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
console.log(process.env);
GET /api/config - - ms - -
在文本编辑器中,我还注意到env变量显示为灰色
const app = express()
const env = process.env.NODE_ENV || “发展”
错误中引用的行如下所示
app.get('/api/config',(req,res) => {
Cosmic.getObject(config,{ slug: 'site-config' },(err,response) => {
const data = response.object.metadata
err ? res.status(500).json({ data: 'error' }) : res.json({ data })
})
})
//cosmicjs/index
getObject: function(config,object,callback){
var endpoint = api_url + '/' + api_version + '/' + config.bucket.slug + '/object/' + object.slug + '?read_key=' + config.bucket.read_key;
if (object._id) {
endpoint = api_url + '/' + api_version + '/' + config.bucket.slug + '/object-by-id/' + object._id + '?read_key=' + config.bucket.read_key;
}
fetch(endpoint)
.then(function(response){
if (response.status >= 400) {
var err = {
'message': 'There was an error with this request.'
}
return callback(err,false);
}
return response.json()
})
.then(function(response){
// Constructor
var cosmic = {};
var object = response.object;
var metafields = object.metafields;
if(metafields){
object.metafield = _.keyBy(metafields,"key");
}
cosmic.object = object;
return callback(false,cosmic);
});
},
这是仓库
https://github.com/cosmicjs/appointment-scheduler
非常感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。