Firebase Cloud Messaging Update Listener Function throws函数返回未定义,预期的承诺或价值

时间:2019-03-17 07:17:58

标签: firebase firebase-cloud-messaging google-cloud-functions

我在节点的特定值更新时编写了Firebase云函数,然后需要触发它。firebase结构和代码如下。我为此使用javascript firebase cli。在firebase控制台中,它不断抛出import AVFoundation import AudioToolbox import Foundation import QuartzCore import Security import WebKit import Cocoa import Metal import MetalKit import Swift let device = MTLCreateSystemDefaultDevice()! // Our clear color, can be set to any color let clearColor = MTLClearColor(red: 0.1, green: 0.57, blue: 0.25, alpha: 1) let nsapp = NSApplication.shared let appName = ProcessInfo.processInfo.processName let window = NSWindow( contentRect: NSMakeRect(0, 0, 1000, 1000), styleMask: .fullSizeContentView, backing: NSWindow.BackingStoreType.buffered, defer: false ) window.cascadeTopLeft(from:NSMakePoint(20,20)) window.title = appName; window.makeKeyAndOrderFront(nil) struct Vertex { var position: float3 var color: float4 } let view = MTKView(frame: NSRect(origin: CGPoint.zero, size: window.frame.size), device: device) window.contentView = view view.device = device view.colorPixelFormat = .bgra8Unorm view.clearColor = clearColor let queue = device.makeCommandQueue()! var vertexBuffer: MTLBuffer! var vertices: [Vertex] = [ Vertex(position: float3(0,1,0), color: float4(1,0,0,1)), Vertex(position: float3(-1,-1,0), color: float4(0,1,0,1)), Vertex(position: float3(1,-1,0), color: float4(0,0,1,1)) ] let shaders = """ #include <metal_stdlib> using namespace metal; // Basic Struct to match our Swift type // This is what is passed into the Vertex Shader struct VertexIn { float3 position; float4 color; }; // What is returned by the Vertex Shader // This is what is passed into the Fragment Shader struct VertexOut { float4 position [[ position ]]; float4 color; }; vertex VertexOut basic_vertex_function(const device VertexIn *vertices [[ buffer(0) ]], uint vertexID [[ vertex_id ]]) { VertexOut vOut; vOut.position = float4(vertices[vertexID].position,1); vOut.color = vertices[vertexID].color; return vOut; } fragment float4 basic_fragment_function(VertexOut vIn [[ stage_in ]]) { return vIn.color; } """ let library = try device.makeLibrary(source: shaders, options: nil) let pipelineDescriptor = MTLRenderPipelineDescriptor() pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm pipelineDescriptor.vertexFunction = library.makeFunction(name: "basic_vertex_function") pipelineDescriptor.fragmentFunction = library.makeFunction(name: "basic_fragment_function") let pipelineState = try device.makeRenderPipelineState(descriptor: pipelineDescriptor) vertexBuffer = device.makeBuffer( bytes: vertices, length: MemoryLayout<Vertex>.stride * vertices.count, options: [] ) enum MetalErrors: Error { case commandBuffer case passDescriptor case encoder } guard let drawable = view.currentDrawable else { throw MetalErrors.commandBuffer } guard let commandBuffer = queue.makeCommandBuffer() else { throw MetalErrors.commandBuffer } guard let passDescriptor = view.currentRenderPassDescriptor else { throw MetalErrors.passDescriptor } guard let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: passDescriptor) else { throw MetalErrors.encoder } nsapp.run() // let vertexData: [Float] = [ -0.5, -0.5, 0, 1, 0, 0, // 0.5, -0.5, 0, 0, 1, 0, // 0, 0.5, 0, 0, 0, 1 ] encoder.setRenderPipelineState(pipelineState) // encoder.setVertexBytes(vertexData, length: vertexData.count * MemoryLayout<Float>.stride, index: 0) encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertices.count) encoder.endEncoding() commandBuffer.present(drawable) commandBuffer.commit() < / p>

Function returned undefined, expected Promise or value

1 个答案:

答案 0 :(得分:1)

1 /您没有正确返回由once()异步方法返回的Promise。

2 /以下几行中也有错误:

  console.log(evnt.after.val);
  const sensData = evnt.after.val;

应该是:

  console.log(evnt.after.val());
  const sensData = evnt.after.val();

因为val()是一种方法

3 /最后,您应该考虑状态为<= 71的情况。

因此,您应该按照以下方式修改代码:

 exports.pressureExceeding = functions.database.ref("Reservoir/{id}")
.onUpdate(evnt => {
  console.log(evnt.after.val);
  const sensData = evnt.after.val;
  const status = sensData.sensor3;
  console.log(evnt.after.val);
  if (status > 71) {
    const payLoad = {
      notification: {
       title: "Emergency Alert",
       body: "{sensData.keys} Pressure is High",
       badge: "1",
       sound: "defualt"  // <- Typo
      }
  };

  //What happens if status <= 71?? You should manage this case, as you are using payload below.

  return admin.database().ref("FcmToken").once("value"). // <- Here return the promise returned by the once() method, then you chain the promises
     .then(allToken => {
       if (allToken.val()) {
         console.log("token available");
         const token = Object.keys(allToken.val());
         return admin.messaging().sendToDevice(token, payLoad);
       } else {
         console.log("no token available");
         return null;   // <- Here return a value
       }
     });
   }
});

最后一句话:对于版本<1.0,您使用的是旧语法。您可能应该更新您的Cloud Function版本(并修改语法)。看看以下文档:https://firebase.google.com/docs/functions/beta-v1-diff