[英]Can't get Firebase update() to work with provided object
Ok, so after a lot of trial and error I have to try asking here. 好的,因此,在经过反复试验之后,我不得不尝试在这里询问。 I'm trying to update a Firebase entry and my code look like this,
我正在尝试更新Firebase条目,我的代码如下所示,
This is from my service.js: 这是从我的service.js:
//This dosn't work
var updateItem = function (item, id) {
var ref = new Firebase(FIREBASE_URI + '/lessons/' + id);
ref.$update(item);
}
//This works (items is a ref to new Firebase(FIREBASE_URI + '/lessons/');
var addItem = function (item) {
items.$add(item);
}
And this is from my controller: 这是从我的控制器来的:
$scope.updateLesson = function (item) {
LessonsService.updateLesson(item, id);
}
The error I'm getting is this: 我得到的错误是这样的:
Error: Firebase.set failed: First argument contains an invalid key ($id). 错误:Firebase.set失败:第一个参数包含无效密钥($ id)。 Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"
键必须是非空字符串,并且不能包含“。”,“#”,“ $”,“ /”,“ [”或“]”
The object I'm passing in looks like this: 我传递的对象如下所示:
Object {
$id: "-JLIUvVMLqoCVgHaed9f",
$bind: function, $add: function, $save: function, $set: function…}
$add: function (item) {
$auth: function (token) {
$bind: function (scope, name, defaultFn) {
$child: function (key) {
$getIndex: function () {
$getRef: function () {
$id: "-JLIUvVMLqoCVgHaed9f"
$off: function (type, callback) {
$on: function (type, callback) {
$remove: function (key) {
$save: function (key) {
$set: function (newValue) {
$transaction: function (updateFn, applyLocally) {
$update: function (newValue) {
createDate: "2014-04-24T11:29:42.692Z"
desc: "test"
somemorevalues: "here"
}
I have tried to do a JSON.stringify on the object and got a nice looking result looking like this: 我尝试在对象上执行JSON.stringify并获得如下所示的漂亮结果:
{
createDate: "2014-04-24T11:29:42.692Z",
desc: "test",
somemorevalues: "here"
}
But it wont accept it when I'm passing that in thru a variable. 但是当我通过一个变量传递它时,它不会接受它。 (Error: Firebase.update failed: First argument must be an object containing the children to replace.) If I pass it in directly as a string it works thou (?!!!).
(错误:Firebase.update失败:第一个参数必须是包含要替换的子代的对象。)如果我直接将其作为字符串传递,则可以使用(?!!!)。 It's the exact same information in the variable.. What the hell am I missing?!
它是变量中完全相同的信息。我到底在想什么? As of now I'm building my own object like this:
到目前为止,我正在构建自己的对象,如下所示:
var newItem = {
title: item.title,
desc: item.desc,
ingress: item.ingress,
workarea: item.workarea,
years: {val: item.years.val},
}
and passing that along to firebase, which works but is not ideal because its "static". 并将其传递给Firebase,虽然可行,但并不理想,因为它的“静态”特性。
Use angularFire according to the API . 根据API使用angularFire。 Read the Angular+Firebase Overview for a good primer.
阅读《 Angular + Firebase概述》以获得良好的入门知识。
In essence, instead of passing angularFire objects into a Firebase ref (which will not work since keys may not have a $ in the name) and calling update/set yourself, simply use the $set
/ $update
methods already available on the $firebase object: 从本质上讲,不要将angularFire对象传递给Firebase引用(因为键名中可能没有$,这将不起作用)并自己调用update / set,而只需使用$ firebase上已经可用的
$set
/ $update
方法宾语:
var ref = $firebase(URL);
ref[id].desc = 'foo';
ref.$save(id).then(onComplete);
Not sure if its just a typo but you've got a comma after the years value (removed it below). 不知道它是否只是一个拼写错误,但是在Years值之后您是否有逗号(在下面将其删除)。
var newItem = {
title: item.title,
desc: item.desc,
ingress: item.ingress,
workarea: item.workarea,
years: {val: item.years.val}
}
As it thinks that its trying to pass an empty key after years, therefor giving the error. 由于它认为它试图在多年后传递一个空密钥,因此给出了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.