我想在移动设备/模拟器上进行调试并将其动态指向API。
const {manifest} = Expo.Constants;
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com'
export function getEvents(){
return fetch('http://${api}/events')
.then(response => response.json())
.then(events => events.map(e =>({ ...e, date: new Date(e.date)})));
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
未定义不是对象(正在评估'_expo.default.Constants')
我在Constants
in处遇到错误const{manifest}=Expo.Constants
并向我显示错误,因为 undefined 不是对象。我无法得到它。任何人都可以帮助我消除该错误。
我正在尝试在我的计算机上运行的 API 和我在手机上调试时建立连接。无论 IP 地址是什么,它都应该连接。
import moment from 'moment';
import Expo from 'expo';
const{ manifest} = Expo.Constants;
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com'
const url ='http://localhost:3000';
export function getEvents(){
return fetch(url)
.then(response => response.json())
.then(events => events.map( e =>({...e})));
}
export function formatDateTime(dateString){
const parsed = moment(new Date(dateString));
if(!parsed.isValid()){
return dateString;
}
return parsed.format('H A on DD MMM YYYY');
}
export function formatDate(dateString){
const parsed = moment(new Date(dateString));
if(!parsed.isValid()){
return dateString; …
Run Code Online (Sandbox Code Playgroud)