gpt4 book ai didi

javascript - 未捕获的类型错误 : Untaming of guest constructed objects unsupported: [object Object]

转载 作者:行者123 更新时间:2023-11-30 16:39:05 26 4
gpt4 key购买 nike

在 Google Apps 脚本中,我在 HTML 页面中有一个表单,它应该在提交时将表单提交给一个函数。但是,当用户单击提交时,此消息会出现在控制台中。

Uncaught TypeError: Untaming of guest constructed objects unsupported: [object Object]

我正在使用 code from Labnol指导

这里是所有的代码:

code.gs

function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

try {

var dropbox = "Student Files";
var folder, folders = DriveApp.getFoldersByName(dropbox);

if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}

var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.myName);

return "File uploaded successfully " + file.getUrl();

} catch (error) {

return error.toString();
}

}

HTML

<?!= include('bootstrap'); ?>
<?!= include('style'); ?>

<div class="container">
<div class="row bottom-margin">
<div class="col-xs-4">
<h1> THE FORM </h1>
</div>
</div>

<div class="span4">
<form id="form">
<div class="row bottom-margin">
<div class="col-xs-6">
<input type="text" class="form-control" name="firstname" placeholder="First name..">
</div>
</div>

<div class="row bottom-margin">
<div class="col-xs-6">
<input type="text" class="form-control" name="lastname" placeholder="Last name..">
</div>
</div>

<div class="row">
<div class="col-xs-6">
<p> How would like to receive your order? </p>
</div>
</div>

<div class="row bottom-margin">
<div class="span12">
<div class="col-xs-6">
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> Mail ($1 charge)
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> Locker Drop Off
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> Meet at next Event etc.
</label>
</div>
</div>
</div>

<div class="row bottom-margin">
<div class="col-xs-4">
<input type="file" class="form-control" name="myFile">
</div>
</div>

<div class="row">
<div class="col-xs-2">
<button type="submit" class="btn btn-primary" value="Upload File"
onclick="this.value='Uploading..';
makeAlert($('#form'));
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode.parentNode.parentNode.parentNode);
return false;">
Submit
</button>
</div>
</div>
</form>
</div>
</div>

<div id="output"></div>

<script>
function makeAlert(item){
console.log(item);
}
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>


<?!= include('jquery'); ?>
<?!= include('bootstrapjs'); ?>

最佳答案

Screen Recording

你报告的错误,Untaming of guest constructed objects unsupported,报告你传递的对象为 "application/x-www-form-urlencoded"形式,不完全是这样。您尝试在传递值之前记录该值是一种很好的本能,但由于控制台最终只显示 [object object] , 帮助不大。

这将有助于确保 makeAlert() 打印的消息可读,通过转换任何 object s 到字符串:

function makeAlert(item){
console.log("makeAlert: "+JSON.stringify(item));
}

有了它,你会在控制台上看到类似这样的东西:

makeAlert: {
"0": {
"0": {},
"1": {},
"length": 2,
"elements": {
"0": {},
"1": {},
"myFile": {}
},
"target": "",
"noValidate": false,
"name": "",
"method": "get",
"encoding": "application/x-www-form-urlencoded",
"enctype": "application/x-www-form-urlencoded",
"autocomplete": "on",
"action": "",
"acceptCharset": ""
},
"length": 1,
"context": {
"location": {
"hash": "",
"search": "",
"pathname": "/userCodeAppPanel",
"port": "",
"hostname": "n-myhzlyl6lgsiepuppv5btyezx3ibnjwczjyz6ii-script.googleusercontent.com",
"host": "n-myhzlyl6lgsiepuppv5btyezx3ibnjwczjyz6ii-script.googleusercontent.com",
"protocol": "https:",
"origin": "https://n-myhzlyl6lgsiepuppv5btyezx3ibnjwczjyz6ii-script.googleusercontent.com",
"href": "https://n-myhzlyl6lgsiepuppv5btyezx3ibnjwczjyz6ii-script.googleusercontent.com/userCodeAppPanel",
"ancestorOrigins": {
"0": "https://n-myhzlyl6lgsiepuppv5btyezx3ibnjwczjyz6ii-script.googleusercontent.com",
"1": "https://script.google.com"
}
},
"jQuery19107126029687933624": 1
},
"selector": "#form"
}

我收到的这个最小化代码的错误与你的略有不同:

Uncaught InvalidArgumentError: Failed due to illegal value in property: 0

非法值(不属于表单)是:

    "jQuery19107126029687933624": 1

查看最小化的 HTML,并重新计算 <button> 的父元素,你会发现只有三个,而不是四个。减少 .parentNode()一个调用,错误就清除了。

这样,文件就被传输到服务器端,并被上传。但是还有另一个错误:

Uncaught TypeError: Cannot read property 'style' of null

这仅仅是因为您的 JavaScript 中的拼写错误,可能是由于剪切和粘贴操作造成的,因为没有名为 'myForm' 的表单.

这是我测试成功的最小化代码:

Index.html

<html>
<div class="container">
<div class="span4">
<form id="form">


<div class="row bottom-margin">
<div class="col-xs-4">
<input type="file" class="form-control" name="myFile">
</div>
</div>

<div class="row">
<div class="col-xs-2">
<button type="submit" class="btn btn-primary" value="Upload File" onclick="this.value='Uploading..';
makeAlert($('#form'));
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode.parentNode.parentNode);
return false;">
Submit
</button>
</div>
</div>
</form>
</div>
</div>

<div id="output"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function makeAlert(item) {
console.log("makeAlert: " + JSON.stringify(item));
}

function fileUploaded(status) {
document.getElementById('form').style.display = 'none'; // No form "myForm"
document.getElementById('output').innerHTML = status;
}
</script>

</html>

代码.gs

问题中没有提供 Google Apps 脚本代码,因此这是对 doGet() 提供的功能的猜测。和 uploadFiles() .

/**
* Serves HTML of the application for HTTP GET requests.
*
* @param {Object} e event parameter that can contain information
* about any URL parameters provided.
*/
function doGet(e) {
var template = HtmlService.createTemplateFromFile('Index');

// Build and return HTML in IFRAME sandbox mode.
return template.evaluate()
.setTitle('Web App Window Title')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}


// adapted from code from http://ctrlq.org/code/19747-google-forms-upload-files
/* This function will process the submitted form */
function uploadFiles(form) {

/* Name of the Drive folder where the files should be saved */
var dropbox = "Dropbox";
var folder, folders = DriveApp.getFoldersByName(dropbox);

/* Find the folder, create if the folder does not exist */
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}

/* Get the file uploaded through the form as a blob */
var blob = form.myFile;
var file = folder.createFile(blob);

/* Return the download URL of the file once its on Google Drive */
return "File uploaded successfully " + file.getUrl();

}

关于javascript - 未捕获的类型错误 : Untaming of guest constructed objects unsupported: [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32279389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com