我正在编写一个使用Google Flat缓冲区的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_genic.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
需求自动存在。但是,对于自定义的flatbuffers.Long
类,默认的flatbuffers.Long.ZERO
会渗入生成的文件中。
虽然这不是一种解决方案,但一种解决方法是将所需的Flatbuffer手动添加到生成的文件中。这很丑陋,但是总比被阻止直到有更合适的答案(或解决方案)更好。
// In `drinks_generated.js`
var flatbuffers = require('../js/flatbuffers').flatbuffers;
注意:
drinks.Drink.addDrinkFlavorId()
和drinks.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
直到更清楚这里发生了什么。附言我很确定该摘要的第30行上的
drinks.createDrinksVector
应该是drinks.Drinks.createDrinksVector
。