簡體   English   中英

無法讀取未定義的屬性“ 0”

[英]Cannot read property '0' of undefined

嘗試使用pkgcloud時出現奇怪的未定義錯誤,並且屬性length未定義:

var pkgcloud = require('pkgcloud');
var Q = require('q');
router.get('/', function(req, res, next) {

var conn = pkgcloud.compute.createClient({
    provider: 'openstack', // required
    username: 'demo', // required
    password: 'azerty', // required
    authUrl: 'http://192.168.0.242:5000' ,
    region_name :'RegionOne'// required
});
console.log(conn);

在這一階段,我可以可視化對象conn的不同方法,但是該錯誤是在for循環內生成的:

var d = Q.defer();
conn.getImages(function(err, images) {

    for (var i=0; i<images.length; i++) {
        console.log("id: " + images[i].id);
        console.log("name: " + images[i].name);
        console.log("created: " + images[i].created);
        console.log("updated: " + images[i].updated);
        console.log("status: " + images[i].status + "\n");
    }
    if (err) {
        console.log("erreur");
        d.reject(err);
    } else {
        console.log("juste");
        d.resolve();
    }
});
  res.render('index', { title: 'Express' });
});

如果要接收對象數組,請考慮使用console.table(); -這樣可以節省您的時間。 就像大家所說的那樣,先檢查錯誤,然后再檢查是否沒有錯誤輸出。 我不知道SO是否會在摘要中呈現表格,因此請檢查瀏覽器的console以獲取表格輸出。

 const img = [{ id: 1, name: "cat", created: '21-07-99', updated: '21-07-99', status: "new" }, { id: 2, name: "dog", created: '26-07-00', updated: '26-07-00', status: "new" }, { id: 3, name: "rabbit", created: '16-02-10', updated: '26-04-10', status: "modified" }]; con = (err, image) => { if (err) { console.log("ERROR", err); return; } console.table(image); }; con("404 not found",img); //error 404 will print err con(null, img); //no error so img 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM