我如何修复此代码中的 "cannot read property match of undefned"
how do i fix "cannot read property match of undefned" in this code
我正在尝试将 nativescript-imagepicker 插件 与我的 Nativescript 应用集成,但出现错误 **无法读取 **属性 匹配未定义** **
以下是我已经尝试过的方法,谢谢。
function onSelectSingleTap(args) {
var context = imagepickerModule.create({
mode: "single"
});
if (platformModule.device.os === "Android" && platformModule.device.sdkVersion >= 23) {
permissions.requestPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE, "I need these permissions to read from storage")
.then(function() {
console.log("Permissions granted!");
startSelection(context);
})
.catch(function() {
console.log("Uh oh, no permissions - plan B time!");
});
} else {
startSelection(context);
}
}
function sendImages(selected) {
let fileUri = selected.fileUri;
imageName = extractImageName(fileUri);
var request = {
url: "http://vvvvvv.com/skog/upload.php",
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": imageName
},
description: "{ 'uploading': " + imageName + " }"
};
//get the image source and upload from there
selected.getImage().then(imageSource => {
let temp = fs.knownFolders.temp();
let uniqueName = '_' + Math.random().toString(36).substr(2, 9);
let filePath = fs.path.join(temp.path, uniqueName + ".jpg");
let saved = imageSource.saveToFile(filePath, enums.ImageFormat.jpeg);
console.log(`item saved:${saved}`);
var task = session.uploadFile(filePath, request);
task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", x => cleanFile(filePath));
});
//return task;
}
function logEvent(e) {
console.log("----------------");
console.log('Status: ' + e.eventName);
console.log('Error: ' + e.error);
// console.log(e.object);
if (e.totalBytes !== undefined) {
console.log('current bytes transfered: ' + e.currentBytes);
console.log('Total bytes to transfer: ' + e.totalBytes);
}
}
function cleanFile(file){
fs.remove(file);
}
function startSelection(context) {
context
.authorize()
.then(function() {
imageItems.length = 0;
return context.present();
})
.then(function(selection) {
selection.forEach(function(selected) {
sendImages(selected);
//selected.uploadTask = sendImages(selected);
selected.imageName = imageName;
console.log("----------------");
console.log("uri: " + selected.uri);
console.log("fileUri: " + selected.fileUri);
console.log('Image name:' + imageName);
imageItems.push(selected);
});
//list.items = selection;
}).catch(function (e) {
console.log(e);
alert(e.message);
});
}
function extractImageName(fileUri) {
var pattern = /[^/]*$/;
var imageName = fileUri.match(pattern);
return imageName[0];
}
我不认为问题出在 php 这就是为什么我没有将代码添加到问题中,但如果您不这么认为,请告诉我
请帮忙
selection
是ImageAsset的数组,所以selected.getImage()
显然是undefined
。它只有 getImageAsync(...)
但 returns 本机图像数据。
要从资产创建 ImageSource,您应该使用 fromAsset
方法。
fileUri
也是 undefined
,没有 属性 这样的。我不确定您是从哪里挑选所有这些的。我建议您参考所有有效属性和方法的文档。
这样就可以了
function onSelectSingleTap(args) {
var context = imagepickerModule.create({
mode: "single"
});
if (platformModule.device.os === "Android" && platformModule.device.sdkVersion >= 23) {
permissions.requestPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE, "I need these permissions to read from storage")
.then(function () {
console.log("Permissions granted!");
startSelection(context);
})
.catch(function () {
console.log("Uh oh, no permissions - plan B time!");
});
} else {
startSelection(context);
}
}
function startSelection(context) {
context
.authorize()
.then(function () {
return context.present();
})
.then(function (selection) {
selection.forEach(function(selected) {
//alert(selected.android.toString());
var file = selected.android.toString();
var url = "https://adekunletestprojects.000webhostapp.com/skog/upload.php";
var name = file.substr(file.lastIndexOf("/") + 1);
//alert(name);
var bghttp = require("nativescript-background-http");
var session = bghttp.session("image-upload");
var request = {
url: url,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": name
},
description: "Uploading " + name
};
var task = session.uploadFile(file, request);
task.on("progress", progressHandler);
return task;
function progressHandler(e) {
var toast = Toast.makeText("uploaded " + e.currentBytes + " / " + e.totalBytes);
toast.show();
}
});
}).catch(function (e) {
console.log(e.eventName);
alert(e.message);
});
}
我正在尝试将 nativescript-imagepicker 插件 与我的 Nativescript 应用集成,但出现错误 **无法读取 **属性 匹配未定义** ** 以下是我已经尝试过的方法,谢谢。
function onSelectSingleTap(args) {
var context = imagepickerModule.create({
mode: "single"
});
if (platformModule.device.os === "Android" && platformModule.device.sdkVersion >= 23) {
permissions.requestPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE, "I need these permissions to read from storage")
.then(function() {
console.log("Permissions granted!");
startSelection(context);
})
.catch(function() {
console.log("Uh oh, no permissions - plan B time!");
});
} else {
startSelection(context);
}
}
function sendImages(selected) {
let fileUri = selected.fileUri;
imageName = extractImageName(fileUri);
var request = {
url: "http://vvvvvv.com/skog/upload.php",
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": imageName
},
description: "{ 'uploading': " + imageName + " }"
};
//get the image source and upload from there
selected.getImage().then(imageSource => {
let temp = fs.knownFolders.temp();
let uniqueName = '_' + Math.random().toString(36).substr(2, 9);
let filePath = fs.path.join(temp.path, uniqueName + ".jpg");
let saved = imageSource.saveToFile(filePath, enums.ImageFormat.jpeg);
console.log(`item saved:${saved}`);
var task = session.uploadFile(filePath, request);
task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", x => cleanFile(filePath));
});
//return task;
}
function logEvent(e) {
console.log("----------------");
console.log('Status: ' + e.eventName);
console.log('Error: ' + e.error);
// console.log(e.object);
if (e.totalBytes !== undefined) {
console.log('current bytes transfered: ' + e.currentBytes);
console.log('Total bytes to transfer: ' + e.totalBytes);
}
}
function cleanFile(file){
fs.remove(file);
}
function startSelection(context) {
context
.authorize()
.then(function() {
imageItems.length = 0;
return context.present();
})
.then(function(selection) {
selection.forEach(function(selected) {
sendImages(selected);
//selected.uploadTask = sendImages(selected);
selected.imageName = imageName;
console.log("----------------");
console.log("uri: " + selected.uri);
console.log("fileUri: " + selected.fileUri);
console.log('Image name:' + imageName);
imageItems.push(selected);
});
//list.items = selection;
}).catch(function (e) {
console.log(e);
alert(e.message);
});
}
function extractImageName(fileUri) {
var pattern = /[^/]*$/;
var imageName = fileUri.match(pattern);
return imageName[0];
}
我不认为问题出在 php 这就是为什么我没有将代码添加到问题中,但如果您不这么认为,请告诉我 请帮忙
selection
是ImageAsset的数组,所以selected.getImage()
显然是undefined
。它只有 getImageAsync(...)
但 returns 本机图像数据。
要从资产创建 ImageSource,您应该使用 fromAsset
方法。
fileUri
也是 undefined
,没有 属性 这样的。我不确定您是从哪里挑选所有这些的。我建议您参考所有有效属性和方法的文档。
这样就可以了
function onSelectSingleTap(args) {
var context = imagepickerModule.create({
mode: "single"
});
if (platformModule.device.os === "Android" && platformModule.device.sdkVersion >= 23) {
permissions.requestPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE, "I need these permissions to read from storage")
.then(function () {
console.log("Permissions granted!");
startSelection(context);
})
.catch(function () {
console.log("Uh oh, no permissions - plan B time!");
});
} else {
startSelection(context);
}
}
function startSelection(context) {
context
.authorize()
.then(function () {
return context.present();
})
.then(function (selection) {
selection.forEach(function(selected) {
//alert(selected.android.toString());
var file = selected.android.toString();
var url = "https://adekunletestprojects.000webhostapp.com/skog/upload.php";
var name = file.substr(file.lastIndexOf("/") + 1);
//alert(name);
var bghttp = require("nativescript-background-http");
var session = bghttp.session("image-upload");
var request = {
url: url,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": name
},
description: "Uploading " + name
};
var task = session.uploadFile(file, request);
task.on("progress", progressHandler);
return task;
function progressHandler(e) {
var toast = Toast.makeText("uploaded " + e.currentBytes + " / " + e.totalBytes);
toast.show();
}
});
}).catch(function (e) {
console.log(e.eventName);
alert(e.message);
});
}