1
所以我在我的API有這樣的:使用取得到一個簡單的字符串
router.get('/assetName', function(req, res, next){
//Get the token from the cookies
var token = req.cookies.access_token;
//Check it with the config secret
if(jwt.verify(token, config.secret)){
//find from name in the request body
User.find({_id: req.query.id}).then(function(users){
var z;
var name = [];
var assetHolder = users[0].assets;
for(z=0; z < assetHolder.length; z++){
if(assetHolder[z]._id == req.query.vid){
name = assetHolder[z].name;
}
}
console.log(name);
res.send(name);
//Error handling
}).catch((err) => console.error(err));
}else{
res.send("Unauthorized");
}
});
名稱變量被打印到安慰,看起來像這樣:
Asset1Name
Asset2Name
我現在用的是取以下請求:
var vehiclesT = asset.assetIDs;
if(!asset.assetIDs){
vehiclesT = [];
}
var y;
var assets = [];
for(y=0; y< assetsTemp.length; y++){
var url = 'api/assetName/?vid='+assetsTemp[y]+'&id='+id;
fetch(url, {credentials: 'include', method: 'get'}).then(function(data){
console.log(data);
vehicles.push(data);
});
}
但將temp被打印爲:
Response {type: "basic", url: "http://localhost:4000/api/assetName/?vid=f4ufh49h49fh9fh94fh94hf&id=f484fh48fhfh98fh489fh", redirected: false, status: 200, ok: true…}
所以車輛陣列是空的。
爲什麼會這樣以及如何使用fetch(或其他更好的方法)將值打印到API中的控制檯?
謝謝,編輯。