简体   繁体   English

Algolia Instant Search Firebase Cloud功能-如何获得其他价值?

[英]Algolia Instant Search Firebase Cloud Function - how to get the other value?

I don't have much idea about JavaScript, so I used Algolia's Instant Search for Firebase Github Repository to build my own function. 我对JavaScript不太了解,因此我使用了Algolia的Firebase Github存储库即时搜索来构建自己的函数。

My function: 我的功能:

exports.indexentry = functions.database.ref('/posts/{postid}/text').onWrite(event => {
  const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
  const firebaseObject = {
    text: event.data.val(),
   timestamp: event.data.val(),
    objectID: event.params.postid
  };

In Algolia indices, with timestamp as the key, I get the same value as in text field, but in Firebase backend timestamp is different. 在以时间戳为键的Algolia索引中,我得到的值与文本字段中的值相同,但是在Firebase后端,时间戳是不同的。 How to fix this? 如何解决这个问题?

I tried different statements to get timestamp value but couldn't. 我尝试了其他语句来获取时间戳记值,但没有。

Edit 编辑

Expected Outcome : 预期成果

{
    text: "random rext",
    timestamp: "time stamp string",
    author: "author name",
    object ID: "object ID"
}

Actual Outcome 实际结果

{
    text: "entered text",
    object ID: "object ID"
}

EDIT 2: Answer can be found here . 编辑2:这里可以找到答案。

Original Answer: What Bob Snyder said about the timestamp event is correct. 原始答案: Bob Snyder关于时间戳事件的说法是正确的。

There may be other fields as well, for example, author_name that we may need to index, is there a generalized way to do that or do I write separate functions for every field? 可能还有其他字段,例如我们可能需要索引的author_name,是否有通用的方法来做到这一点,或者我为每个字段编写单独的函数?

If you want a general way to add all fields, I think what you are looking for can be found here . 如果您想以一般方式添加所有字段,可以在这里找到您要查找的内容。 This should give you the right guidance to get what you want, ie save your whole object into the Algolia index. 这应该为您提供所需的正确指导,即将整个对象保存到Algolia索引中。

EDIT: 编辑:

index.saveObject(firebaseObject, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase object indexed in Algolia', firebaseObject.objectID);
  });

I'm not real clear about your goal. 我不清楚您的目标。 Event has a timestamp property. 事件具有时间戳属性。 Have you tried: 你有没有尝试过:

  const firebaseObject = {
    text: event.data.val(),
    timestamp: event.timestamp, // <= CHANGED
    objectID: event.params.postid
  };

If you want a long instead of string , use Date.parse(event.timestamp) 如果要使用long而不是string ,请使用Date.parse(event.timestamp)

event.data.val() returns the entire firebase snapshot. event.data.val()返回整个Firebase快照。 If you want a specific value in your data you add it after .val() for example if every post has an author stored in your firebase database under they key "author" you can get this value using var postAuthor = event.data.val().author 如果您要在数据中添加特定值,则可以在.val()之后添加该值,例如,如果每个帖子的.val()数据库中都有一个作者存储在其关键字“作者”下,则可以使用var postAuthor = event.data.val().author获取此值。 var postAuthor = event.data.val().author

I've included some samples from my code for those interested. 我从代码中为感兴趣的人提供了一些示例。 A sample post looks like this: 示例帖子如下所示: 在此处输入图片说明

Then inside my cloud functions I can access data like this: 然后,在我的云函数内部,我可以像这样访问数据:

const postToCopy = event.data.val(); // entire post
const table = event.data.val().group;
const category = event.data.val().category;
const region = event.data.val().region;
const postKey = event.data.val().postID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM