[英]How to add data to firebase using node js?
I am creating an admin panel where the user enters car details and it maps to my firebase real-time database.When the user hits save, the input fields has should be mapped to the database.我正在创建一个管理面板,用户在其中输入汽车详细信息,它映射到我的 firebase 实时数据库。当用户点击保存时,输入字段应该映射到数据库。 I have tried everything out there but it simply wont work.
我已经尝试了一切,但它根本行不通。
When I hit on save, i get the following error:当我点击保存时,我收到以下错误:
validation.ts:145 Uncaught Error: Reference.set failed: First argument contains undefined in property 'cars.New car.newCar.transmission'
at rc (validation.ts:145)
at validation.ts:204
at Cu (util.ts:417)
at rc (validation.ts:185)
at validation.ts:204
at Cu (util.ts:417)
at rc (validation.ts:185)
at Hu (validation.ts:122)
at Df.set (Reference.ts:137)
at addData (addcar.html:96)
The following is my html and javascript code.以下是我的 html 和 javascript 代码。
//config is add here // initialize firebase firebase.initializeApp(firebaseConfig); //this.database = firebase.database(); function addData() { var databaseRef= firebase.database().ref(); var storesRef =databaseRef.child('Cars/new car'); var addCar = storesRef.push(); addCar.set({ name:$("#car-name").val(), transmission:$("#car_transmission").val() }); }
<head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script> <script src="https://www.gstatic.com/firebasejs/7.13.2/firebase.js"></script> </head> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container-fluid"> <a class="navbar-brand" href="/public/admin.html"> Rentacabs Admin </a> <div class="dropdown navbar-right"> <button id="user-email" class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" > incubsolutions@gmail.com </button> <div class="dropdown-menu" aria-labeledby="user-email" > <a class="dropdown-item" id="btn-logout" href="#"> Logout </a> </div> </div> </div> </nav> <h2>Cars</h2> <hr /> <div class="row"> <div class="col-lg-5"> <h4>Add new</h4> <form id="category-form"> <div class="form-group"> <label for="car-name">Enter car name</label> <input type="text" class="form-control" id="car-name" /> </div> <div class="form-group"> <label for="fuel_type">Fuel Type</label> <select name="" class="form-control" id="fuel_type"> <option value="petrol">Petrol</option> <option value="diesel">Diesel</option> </select> </div> <div class="form-group"> <label for="car_milege">Milege</label> <input type="text" class="form-control" id="car_milege" /> </div> <div class="form-group"> <label for="car_transmission">Transmission</label> <select name="" class="form-control" id="fuel_type"> <option value="automatic">Automatic</option> <option value="Manual">Manual</option> </select> </div> <div class="form-group"> <label for="car_rate">rate per km</label> <input type="text" class="form-control" id="car_rate" /> </div> <div class="from-group"> <button id="save-car" type="submit" class="btn btn-primary" onclick="addData()">Save</button> </div> </form> </div> </div>
any help is appreciated, Thanks in advance.任何帮助表示赞赏,在此先感谢。
This is because you reuse the fuel_type
id for your transmission
field:这是因为您为
transmission
字段重用了fuel_type
id:
<label for="car_transmission">Transmission</label>
<select name="" class="form-control" id="fuel_type">
//....
And then you have another problem: your button is of type type="submit"
.然后你有另一个问题:你的按钮是
type="submit"
。 Your form is submitted before the Firebase method is triggered.在触发 Firebase 方法之前提交您的表单。 You should change the button type to
button
.您应该将按钮类型更改为
button
。 See the W3 specification for more detail on button types.有关按钮类型的更多详细信息,请参阅W3 规范。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.