我對NodeJs非常陌生,我正在製作一個快速應用程序來學習,在這個應用程序中,我想利用用戶提供的經度和緯度來通過節點地理編碼器反向地址解析地址,下面的代碼允許我將模型保存在數據庫中。我想讓用戶知道過程是否成功,我如何從save函數中的狀態獲取值並將其傳遞給響應?如何從NodeJS的回調函數中獲取值?
在此先感謝。
app.post('/persons', function (req, res){
var createPersonWithGeocode = function (callback){
var lat=req.body.latitude;
var lon=req.body.longitude;
var status;
function geocodePerson() {
geocoder.reverse(lat,lon,createPerson);
}
function createPerson(err, geo) {
var geoPerson = new Person({
name: req.body.name,
midname: req.body.midname,
height: req.body.height,
gender: req.body.gender,
age: req.body.age,
eyes: req.body.eyes,
complexion: req.body.complexion,
hair: req.body.hair,
latitude: req.body.latitude,
longitude: req.body.longitude,
geocoding: JSON.stringify(geo),
description:req.body.description,
});
geoPerson.save(function (err) {
if (!err) {
console.log("Created");
status="true";
} else {
console.log(err);
status="false";
}
});
}
geocodePerson();
}
return res.send(createPersonWithGeocode());
});
讓我猜一猜:你還需要掌握異步編碼的概念嗎? (這裏有一段時間,使用它們:............) – MaxArt
感謝您的回答最大。我對這段時間感到抱歉,但英語不是我的第一語言(雖然不是藉口)。再次感謝您的幫助:) – torresomar
不客氣。而且你的英語畢竟還好(但是,嘿,我也不是英語母語的人)。 – MaxArt