首先,我想说我的很多代码可能很恐怖,因此我深表歉意。我的.ts和.js经验不足以使我感到非常自在(因此,我无法解决此问题)。因此,我正在尝试将文档内容从一个文档转移到另一个文档。目前,我通过快照来完成此操作,但是我敢肯定有更快更好的方法。我的代码在下面。
当我尝试将第二个集合(redacted2)的文档设置为快照数据时,发生错误。从控制台日志中可以看出,我知道快照.data()具有我需要的有价值的信息,其格式完全符合我的期望。我假设这类似于迅速创建一个保护语句,在该语句中我可以检查null值并将其分配给给定类型(但在这里我可能是错误的)。
db.collection("Redacted").doc(context.params.sensors).get()
.then(function(querySnapshot){
console.log("Query Snapshot is is isssss: ", querySnapshot)
console.log("Query Snapshot querySnapshot.data: ", querySnapshot.data)
console.log("Query Snapshot querySnapshot.data(): ", querySnapshot.data())
console.log("Query Snapshot querySnapshot.ref: ", querySnapshot.ref)
console.log("Query Snapshot querySnapshot.get: ", querySnapshot.get)
return querySnapshot
}).then(function(querySnapshotData) {
console.log("ayoooooo blooooddd", querySnapshotData)
db.collection("Redacted2").doc(context.params.sensors).set(querySnapshotData.data())
.then((alertResponse1: any) => {
console.log("Successfully updated firestore: ", alertResponse1)
})
.catch ((alertError1: any) => {
console.log("Successfully updated firestore but with error: ", alertError1)
})
})
.catch(function(error) {
console.log("query snapshot error: ", error)
});
答案 0 :(得分:1)
从API documentation for DocumentSnapshot.data()中可以看到,<div class="container">
<div class="row">
@foreach($questions as $question)
@if($id == $question->category_id)
<legend> Quiz Of {{ $question->category->title }}</legend>
<form method="POST" action="{{ url('/store-answer') }}" class="crud-submit" id="quiz_module">
<div class="jumbotron">
<input type="hidden" name="user_id" value="{{ auth()->user()->id }}">
<input type="hidden" name="category_id" value="{{ $question->category_id }}">
<input type="hidden" name="question_id" value="{{ $question->id }}">
<h4>
Question {{ $question->id }}
</h4>
<h3>
{{ $question->question }}
</h3><brx`>
<h5>
 <input type="radio" name="answer" class="form-check-input" value="{{ $question->option_a }}">{{ $question->option_a }}<br>
 <input type="radio" name="answer" class="form-check-input" value="{{ $question->option_b }}">{{ $question->option_b }}<br>
 <input type="radio" name="answer" class="form-check-input" value="{{ $question->option_c }}">{{ $question->option_c }}<br>
 <input type="radio" name="answer" class="form-check-input" value="{{ $question->option_d }}">{{ $question->option_d }}<br>
</h5>
</div>
</form>
@endif
@endforeach
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("input[type='radio']").click(function()
{
var radioValue = $("input[name='answer']:checked").val();
//$(".crud-submit").click(function (e) {
// e.preventDefault();
var form_action = $("#quiz_module").find("form").attr("action");
var user_id = $("input[name='user_id']").val();
var category_id = $("input[name='category_id']").val();
var question_id = $("input[name='question_id']").val();
//var radioValue = $("input[name='answer']:checked").val();
$.ajax({
type: 'POST',
dataType: 'json',
url: '/store-answer',
data: { user_id: user_id, category_id: category_id, question_id: question_id, radioValue: radioValue },
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
});
//});
});
});
</script>
被声明为返回data()
或DocumentData
。如果文档不存在,则可能为undefined
。 TypeScript不允许您在可能为undefined
的对象上调用方法,因为这会在运行时生成错误。您将必须确保TypeScript的结果实际上是undefined
,而不是DocumentData
。
undefined