1

我正在编写一个使用谷歌平面缓冲区的 nodeJs 应用程序。

我在我的 macbook pro 上安装了 flatc 并编译了以下架构:

命名空间 MyAlcoholist;

table Drink {
 drink_type_name: string;
 drink_company_name: string;
 drink_brand_name: string;
 drink_flavor_type_name : string;
 liquid_color_type_name : string;
 liquid_color_is_transparent : bool;
 alcohol_vol : float;
 calories_for_100g : uint;
 global_image_id: ulong;
 drink_flavor_id: ulong;
}

table Drinks { drinks:[Drink]; }

root_type Drinks;

架构文件名为drink.fbs,它生成了一个名为drink_generated.js 的javascript 文件

我将此文件包含在我的 nodejs 应用程序中,并使用以下代码向其中添加数据。这是我的 flatBufferUtil.js 实用程序文件。

var flatbuffers = require('../js/flatbuffers').flatbuffers;
var builder = new flatbuffers.Builder();

var drinks = require('../fbs/drinks_generated').MyAlcoholist; // Generated by `flatc`.

function drinkArrayToBuffer(drinkArray) {
var drinksVectArray = [];
drinkArray.forEach(function (element, index, array) {
    var drinkObj = element;
    var drinkBrandName = builder.createString(drinkObj.drink_brand_name);
    var drinkCompanyName = builder.createString(drinkObj.drink_company_name);
    var drinkflavorTypeName = builder.createString(drinkObj.drink_flavor_type_name);
    var drinkTypeName = builder.createString(drinkObj.drink_type_name);
    var liquidColorTypeName = builder.createString(drinkObj.liquid_color_type_name);
    drinks.Drink.startDrink(builder);
    drinks.Drink.addAlcoholVol(builder, drinkObj.alcohol_vol);
    drinks.Drink.addCaloriesFor100g(builder,drinkObj.calories_for_100g);
    drinks.Drink.addDrinkBrandName(builder,drinkBrandName);
    drinks.Drink.addDrinkCompanyName(builder,drinkCompanyName);
    drinks.Drink.addDrinkFlavorId(builder,drinkObj.drink_flavor_id);
    drinks.Drink.addDrinkFlavorTypeName(builder, drinkflavorTypeName);
    drinks.Drink.addDrinkTypeName(builder,drinkTypeName);
    drinks.Drink.addGlobalImageId(builder,drinkObj.global_image_id);
    drinks.Drink.addLiquidColorIsTransparent(builder,drinkObj.is_transparent);
    drinks.Drink.addLiquidColorTypeName(builder,liquidColorTypeName);
    var drink = drinks.Drink.endDrink(builder);
    drinksVectArray.push(drink);
})
var drinksVect = drinks.createDrinksVector(builder,drinksVectArray);
builder.finish(drinksVect);
var buf = builder.dataBuffer();
return buf;
}

module.exports.drinkArrayToBuffer=drinkArrayToBuffer;

现在,当我执行此功能时,它会因错误而失败flatbuffers is not defined

我调试了我的代码,我看到它在以下代码行中归档:

    drinks.Drink.addDrinkFlavorId(builder,drinkObj.drink_flavor_id);

如果我进入 addDrinkFlavorId 函数,我会在drinks_generted.js 中看到这段代码:

MyAlcoholist.Drink.addDrinkFlavorId = function(builder, drinkFlavorId) {
   builder.addFieldInt64(9, drinkFlavorId, flatbuffers.Long.ZERO);
};

正如您所看到的,它使用flatbuffers.Long.ZERO了 flatbuffers,但该文件中根本没有定义 flatbuffers。编译没有提供任何错误,所以我错过了什么?

4

1 回答 1

1

在我看来,这似乎是一个错误......生成的文件似乎是从flatbuffers要求中自主存在的。但是对于自定义flatbuffers.Long类,默认值会flatbuffers.Long.ZERO渗入生成的文件中。

虽然这不是一个解决方案,但一种解决方法是手动将 flatbuffers 需要添加到生成的文件中;这很丑陋,但它可能比在更合适的答案(或修复)出现之前被阻止更好。

// In `drinks_generated.js`
var flatbuffers = require('../js/flatbuffers').flatbuffers;

笔记:

drinks.Drink.addDrinkFlavorId()anddrinks.Drink.addGlobalImageId()函数期望将flatbuffers.Long值传递给它们,因为它们是ulong在模式(fbs文件)中指定的。因此,您需要确保您没有尝试传递简单number类型。

例如:

var my_long = flatbuffers.Long(100, 0); // low = 100, high = 0
drinks.Drink.addDrinkFlavorId(builder, my_long);

因此,另一种可能的解决方法是更改​​架构中这些字段的数据类型以避免使用ulong,直到这里发生的事情变得更加清楚。

PS 我很确定drinks.createDrinksVector该片段的第 30 行应该是drinks.Drinks.createDrinksVector.

于 2016-02-19T08:55:18.903 回答