我想从cloud firestore中获取一些数据,然后在地图中返回这些数据。问题是,系统在获取值之前返回该值,并且promise处于挂起状态。我尝试了很多方法,但都没能解决这个问题。如果有人能帮我就太好了!
function homepage(formData) {
var cfire = cfirebase.firestore().collection('Details').doc('dsadsadasdsadasdsdsdasd');
const test = cfire.get().then(doc => {
if (doc.exists) {
Name = doc.data().dd.dn;
vehical = doc.data().dd.vn;
vehicalNumber = doc.data().dd.vno;
console.log(Name, vehical, vehicalNumber);
} else if (!doc.exists) {
console.log("Detail does not exist");
return Promise.reject(new Error('Detail does not exist'));
} else {
throw Promise.reject(new Error('Server error!'));
}
}).catch(e => {
return false
});
return {
tripID: {
c: false,
dd: {
// Driver name
dn: Name,
// Vehical name
vn: vehical,
// Vehical number
vno: vehicalNumber,
}
}
};
}
函数在从cloud firestore获取数据之前返回对象的原因是cfirebase.firestore().collection(...).doc(...)
是异步的,它返回一个Promise
,所以在返回最终对象之前,必须等待Promise
解析。
如果你不熟悉的概念,你应该阅读asynchronous programming
- 异步编程
我建议您更改homepage
函数,使其返回一个Promise
,或者使用async/await
等待cfire.get()
用某个值解析,然后再将return
作为最终对象。
选项#1
function homepage(formData) {
const cfire = cfirebase.firestore().collection(...).doc(...)
return cfire.get()
.then(doc => {
if (!doc) {
throw new Error("Server error")
}
if (!doc.exists) {
throw new Error("Details does not exist")
}
// ...
return { Name, vehical, vehicalNumber }
})
.then(result => {
// result => { Name, vehical, vehicalNumber }
// ...
return {
tripId: {...}
}
})
}
// then
homepage(...)
.then(result => {...})
.catch(error => {...})
选项#2
要使用async/await
,您必须具有Node.js 8.x
或更高版本
async function homepage(formData) {
try {
const cfire = cfirebase.firestore().collection(...).doc(...)
const doc = await cfire.get()
if (!doc) {
throw new Error("Server error")
}
if (!doc.exists) {
throw new Error("Details does not exist")
}
// ...
return {
tripID: {...}
}
} catch(error) {
throw error
}
}
// then
homepage(...)
.then(result => {...})
.catch(error => {...})
阅读有关async/await
:的更多信息
- 异步/等待
希望这能有所帮助。
这是因为JS的异步特性。在".then"块中编写返回语句。
function homepage(formData) {
var cfire = cfirebase.firestore().collection('Details').doc('dsadsadasdsadasdsdsdasd');
const test = cfire.get().then(doc => {
if (doc.exists) {
Name = doc.data().dd.dn;
vehical = doc.data().dd.vn;
vehicalNumber = doc.data().dd.vno;
console.log(Name, vehical, vehicalNumber);
return {
tripID: {
c: false,
dd: {
// Driver name
dn: Name,
// Vehical name
vn: vehical,
// Vehical number
vno: vehicalNumber,
}
}
};
} else if (!doc.exists) {
console.log("Detail does not exist");
return Promise.reject(new Error('Detail does not exist'));
} else {
throw Promise.reject(new Error('Server error!'));
}
}).catch(e => {
return false
});
}
你可以这样做
function homepage(formData) {
return new Promise((resolve, reject) => {
var cfire = cfirebase.firestore().collection('Details').doc('dsadsadasdsadasdsdsdasd');
var returnObj = {
tripID: {
c: false,
dd: {
// Driver name
dn: '',
// Vehical name
vn: '',
// Vehical number
vno: '',
}
}
}
const test = cfire.get().then(doc => {
if (doc.exists) {
returnObj.tripID.dd.dn = doc.data().dd.dn;
returnObj.tripID.dd.vn = doc.data().dd.vn;
returnObj.tripID.dd.vno = doc.data().dd.vno;
console.log('returnObj => ', returnObj);
resolve(returnObj);
} else if (!doc.exists) {
console.log("Detail does not exist");
reject('Detail does not exist');
} else {
reject('Server error!');
}
}).catch(e => {
reject('Server error!');
});
});
}
并调用类似的函数
homepage().then((res) => {
console.log('res => ', res);
}).catch((err) => {
console.log(err)
});