[英]passing array as reference type node.js
我正在連接到S3並解析出一個json文件,
我創建了一個名為Singer的新對象和一個對象數組。 我想在功能范圍之外使用此對象數組
console.log("Loading up the best code ever!!!");
var fs = require('fs');
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
var jsonfile = require('jsonfile')
var Singer = require('./Singer')
// Set the region
AWS.config.update({ region: "us-west-1" });
var credentials = new AWS.SharedIniFileCredentials();
AWS.config.credentials = credentials;
// Create S3 service object
s3 = new AWS.S3({ apiVersion: '2006-03-01' });
console.log("after S3");
// Create the parameters for calling createBucket
var bucketParams = {
Bucket: 'pc-backend-exercises',
Key: 'toSearch.json',
ResponseContentType: 'application/json'
};
s3.getObject(bucketParams, function (err, data) {
// Handle any error and exit
if (err) {
console.log(err, err.stack);
return err;
}
var fileContents = data.Body.toString();
var json = JSON.parse(fileContents);
console.log(json);
var singers = [];
for (var i = 0; i < json.Search.artists.length; i++) {
var newSinger = new Singer(json.Search.artists[i]);
singers.push(newSinger);
}
console.log('Singers:');
console.log(singers);
});
console.log('download json file from s3');
此代碼可以正常工作。 我得到了輸出
Loading up the best code ever!!!
after S3
download json file from s3
{ Search: { artists: [ 'Katy', 'Madonna', 'Rihanna', 'Beyonce' ] } }
Singers:
[ Singer { name: 'Katy', songs: [] },
Singer { name: 'Madonna', songs: [] },
Singer { name: 'Rihanna', songs: [] },
Singer { name: 'Beyonce', songs: [] } ]
但是,我更改代碼以通過引用傳遞數組歌手的方法卻行不通。
var singers = [];
s3.getObject(bucketParams, function (err, data, singers) {
// Handle any error and exit
if (err) {
console.log(err, err.stack);
return err;
}
var fileContents = data.Body.toString();
var json = JSON.parse(fileContents);
console.log(json);
for (var i = 0; i < json.Search.artists.length; i++) {
var newSinger = new Singer(json.Search.artists[i]);
singers.push(newSinger);
}
});
console.log('Singers:');
console.log(singers);
console.log('download json file from s3');
輸出為:
Loading up the best code ever!!!
after S3
Singers:
[]
download json file from s3
{ Search: { artists: [ 'Katy', 'Madonna', 'Rihanna', 'Beyonce' ] } }
C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:31
throw err;
^
TypeError: Cannot read property 'push' of undefined
at Response.<anonymous> (C:\Users\gdarmon\Desktop\Node\gili.js:38:17)
at Request.<anonymous> (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:364:18)
at Request.callListeners (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\sequential_executor.js:105:20)
at Request.emit (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\sequential_executor.js:77:10)
at Request.emit (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:683:14)
at Request.transition (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\state_machine.js:14:12)
at C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (C:\Users\gdarmon\Desktop\Node\node_modules\aws-sdk\lib\request.js:685:12)
你能幫忙嗎?
您在這里有兩個問題。
無法讀取未定義的屬性“ push”
看這里:
s3.getObject(bucketParams, function (err, data, singers) {
singers
是未定義的,因為您定義了一個稱為singers
的參數(它將在更大范圍內屏蔽該變量)。 getObject
函數在調用傳遞給它的匿名函數時不會給第三個參數賦值。
我不知道你為什么期望它。
如果刪除該參數,則可以再次訪問范圍更廣的singers
變量。
這是一個常見問題解答。 請參閱如何從異步調用返回響應? 這可能是所有Stackoverflow上“以重復方式關閉”的前3個目標。
問題是變量的范圍。 您已經在本地和全局中定義了相同的變量名。
更改局部變量的名稱,它應該工作我有變化singers1
var singers = [];
s3.getObject(bucketParams, function (err, data, singers1) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.